There are two ways of doing that...
Example #1, the best method:
Code: Select all
$sql = 'SELECT *
FROM ' . SOME_TABLE . "
WHERE some_field = '" . $db->sql_escape($some_value) . "'";
$result = $db->sql_query($sql);
$s_option_list = '';
$i = 1;
while ($row = $db->sql_fetchrow($result))
{
$s_option_list .= '<option value="' . $i . '">' . $row['some_field'] . '</option>';
$i++;
}
$db->sql_freeresult($result);
$template->assign_var('S_OPTION_LIST', $s_option_list);
Template:
Code: Select all
<select name="option_list" id="option_list">{S_OPTION_LIST}</select>
Example #2:Code: Select all
$sql = 'SELECT *
FROM ' . SOME_TABLE . "
WHERE some_field = '" . $db->sql_escape($some_value) . "'";
$result = $db->sql_query($sql);
$s_option_list = '<select name="option_list" id="option_list">';
$i = 1;
while ($row = $db->sql_fetchrow($result))
{
$s_option_list .= '<option value="' . $i . '">' . $row['some_field'] . '</option>';
$i++;
}
$db->sql_freeresult($result);
$s_option_list .= '</select>';
$template->assign_var('S_OPTION_LIST', $s_option_list);
Template:
Note that we correctly use $db->sql_fetchrow(), we use TABLE_CONSTANT for the table name, and we use a while loop instead of of a for loop for a database call.
In the first example, you're letting the template control the select box, as you may want different boxes containing the same data, in the second example, you would use that if you knew that no other box on a single page would contain that drop-down.
Since we are assigning only asingle var, we use assign_var() instead of assign_vars(array());