Wordpress在选择选项列表中获取行和显示

时间:2014-09-13 21:57:02

标签: sql wordpress while-loop

这是我到目前为止所得到的:

global $wpdb;    
$sql = "SELECT Cause FROM `wp_test` WHERE Accepted = 1 ORDER BY Name";

$res = $wpdb->get_results($sql);


echo "<select name = 'venue'>";
while (($row = mysql_fetch_row($res)) != null)
{
    echo "<option value = '{$row[???]}'";
    if ($selected_venue_id == $row[???])
        echo "selected = 'selected'";
    echo ">{$row['???']}</option>";
}
echo "</select>";

while循环之前的代码有效。如何使while循环返回我的列?

1 个答案:

答案 0 :(得分:0)

如果您的查询为WPDB课程,则不需要使用mysql_*家庭功能,所有工作都由此课程完成,您只需要循环$res它包含查询返回的数据

global $wpdb;
$sql = "SELECT Cause FROM `wp_test` WHERE Accepted = 1 ORDER BY Name";
$res = $wpdb->get_results($sql);
if (!empty($res)) {
    echo "<select name = 'venue'>";
    foreach ($res as $key => $val) {
        echo "<option value = '" . $val->Cause . "'";
        if ($selected_venue_id == $val->Cause)
            echo "selected = 'selected'";
        echo ">" . $val->Cause . "</option>";
    }
    echo "</select>";
}