html_options返回double值

时间:2011-05-09 11:47:58

标签: smarty

我想通过使用html_options在smarty中选择多个选项。这是我的代码:

<?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>
{html_options values=$featured_id output=$featured_name selected=$featured_id}
</select>

输出:

<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>

如何解决这个问题???

1 个答案:

答案 0 :(得分:0)

很可能sql_fetchrowset()没有返回单个维度数组,这些属性需要具有简单的下拉列表。我要在黑暗中刺伤,试试这个:

<?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>

您不需要在Smarty {htmloptions}中使用valuesoutputoptions会使用关联数组。您可能正在将数组数组传递给每个属性。