html_options return double value
I want to select multiple options in smarty by using html_options. here is my code:
<?php
$sql_i = "SELECT id FROM ".$GLOBALS['table']['property']." WHERE featured = 'Y' ORDER BY `id` DESC";
$res_i= $GLOBALS['db']->sql_query($sql_i);
$smarty->assign('featured_name', $GLOBALS['db']->sql_fetchrowset($res_i));
$sql_p = "SELECT pname FROM ".$GLOBALS['table']['property']." WHERE featured = 'Y' ORDER BY `id` DESC";
$res_p = $GLOBALS['db']->sql_query($sql_p);
$smarty->assign('featured_id', $GLOBALS['db']->sql_fetchrowset($res_p));
?>
<select name="property_id">
<option value="">---Select--</option>
{h开发者_如何学Pythontml_options values=$featured_id output=$featured_name selected=$featured_id}
</select>
Output:
<select name="property_id">
<option value="">---Select--</option>
<optgroup label="Array">
<option label="Plaza del Sol" value="0">Plaza del Sol</option>
<option label="Plaza del Sol" value="pname">Plaza del Sol</option>
</optgroup>
<optgroup label="Array">
<option label="Plaza Del Sol" value="0">Plaza Del Sol</option>
<option label="Plaza Del Sol" value="pname">Plaza Del Sol</option>
</optgroup>
<optgroup label="Array">
<option label="Park Terrace " value="0">Park Terrace </option>
<option label="Park Terrace " value="pname">Park Terrace </option>
</optgroup>
<optgroup label="Array">
<option label="Park Terrace 1" value="0">Park Terrace 1</option>
<option label="Park Terrace 1" value="pname">Park Terrace 1</option>
</optgroup>
<optgroup label="Array">
<option label="test" value="0">test</option>
<option label="test" value="pname">test</option>
</optgroup>
</select>
how to solve this problem???
Most likely sql_fetchrowset()
is not returning a single dimension array, which you need for those attributes to have a simple dropdown. I'm going to take a stab in the dark here, try this:
<?php
// You don't need two identical queries
$query = "SELECT pname,id FROM ".$GLOBALS['table']['property']." WHERE featured = 'Y' ORDER BY `id` DESC";
$result = $GLOBALS['db']->sql_query($query);
$featured_id = '';
foreach ($GLOBALS['db']->sql_fetchrowset($result) as $row):
// Assuming $row is an object, use appropriate syntax otherwise
$options[$row->id] = $row->pname;
// If you know what the featured id is, use this
// if ($row->id == $my_featured_id):
// Or maybe featured_id is INT(1) OR BOOLEAN value in your table?
// Make sure to SELECT it if this is the case
// Also make sure you handle the possibility of two featured ids in one loop
if ($row->featured_id == 1):
$featured_id = $row->id;
endif;
endforeach;
$smarty->assign('options', $options);
$smarty->assign('featured_id', $featured_id);
?>
<select name="property_id">
<option value="">---Select--</option>
{html_options options=$options selected=$featured_id}
</select>
You don't need to use values
and output
in Smarty {htmloptions}, options
with an associative array will do. You're probably passing an array of arrays to each attribute.
精彩评论