1

我尝试从包含主要项目及其子项目的数据库中创建一个下拉列表。我希望主要项目成为 optgroup 标签,子项目成为选项项目。

<select>
  <optgroup label="$rs_pa['Name']">
    <option value="$rs_ch['Link']">$rs_ch['Name']</option>
    <option value="$rs_ch['Link']">$rs_ch['Name']</option>
  </optgroup>
  <optgroup label="$rs_pa['Name']">
    <option value="$rs_ch['Link']">$rs_ch['Name']</option>
    <option value="$rs_ch['Link']">$rs_ch['Name']</option>
  </optgroup>
</select>

以上只是显示每个变量应适合的位置的方案。

$rs_pa['Name] - 是父项的名称。

$rs_ch['Name] - 是子项的名称。

$rs_ch['Link'] - 是子项的链接。

这是我现在拥有的代码:

$mysql_table_items      = "sys_menu_top";
$mysql_table_options    = "sys_options";

// SQL QUERY
$mysql_result_number    = mysql_query("SELECT VALUE FROM $mysql_table_options WHERE Name LIKE 'nav_menu_elements_on_line_usr'") or die($myQuery . "<br/><br/>" . mysql_error());
$mysql_select_parent    = "SELECT * FROM $mysql_table_items WHERE `Parent` = '0' AND Type LIKE 'top' AND Active = 1 ORDER BY `Order` ASC";
$mysql_select_child     = "SELECT * FROM $mysql_table_items WHERE `Parent` = '" . $rs_pa['ID'] . "' AND Active = 1 ORDER BY `Order` ASC";

// SQL RESULT
$mysql_result_number    = mysql_fetch_array($mysql_result_number, MYSQL_ASSOC) or die("Error: Query Failed! " . mysql_error());
$mysql_result_parent    = mysql_query($mysql_select_parent) or die("Error: Query Failed! " . mysql_error());
$mysql_result_child     = mysql_query($mysql_select_child) or die("Error: Query Failed! " . mysql_error());

$output .= "<select>";
while ($rs_pa = mysql_fetch_array($mysql_result_parent)) {
    $output .= "<optgroup label='" . $rs_pa['Name'] . "'>";
    while ($rs_ch = mysql_fetch_array($mysql_result_child)) {
        $output .= "<option value='" . $rs_ch['Link'] . "'>" . $rs_ch['Name'] . "</option>";
        }
    $output .= "</optgroup>";
    }

$output .= "</select>";

echo $output;

问题是这显示不正确..我尝试了一切,但我对php不太好..谢谢你的帮助..

4

1 回答 1

1

$rs_pa['ID']在此行引用:

$mysql_select_child     = "SELECT * FROM $mysql_table_items WHERE `Parent` = '" . $rs_pa['ID'] . "' AND Active = 1 ORDER BY `Order` ASC";

你确定$rs_pa['ID']是在那个时候定义的吗?

这样的事情应该会更好:

$mysql_table_items      = "sys_menu_top";
$mysql_table_options    = "sys_options";

// SQL QUERY
$mysql_result_number    = mysql_query("SELECT VALUE FROM $mysql_table_options WHERE Name LIKE 'nav_menu_elements_on_line_usr'") or die($myQuery . "<br/><br/>" . mysql_error());
$mysql_select_parent    = "SELECT * FROM $mysql_table_items WHERE `Parent` = '0' AND Type LIKE 'top' AND Active = 1 ORDER BY `Order` ASC";

// SQL RESULT
$mysql_result_number    = mysql_fetch_array($mysql_result_number, MYSQL_ASSOC) or die("Error: Query Failed! " . mysql_error());
$mysql_result_parent    = mysql_query($mysql_select_parent) or die("Error: Query Failed! " . mysql_error());

$output = "<select>";

while ($rs_pa = mysql_fetch_array($mysql_result_parent))
{
    $output .= "<optgroup label='" . $rs_pa['Name'] . "'>";

    $mysql_select_child     = "SELECT * FROM $mysql_table_items WHERE `Parent` = '" . $rs_pa['ID'] . "' AND Active = 1 ORDER BY `Order` ASC";
    $mysql_result_child     = mysql_query($mysql_select_child) or die("Error: Query Failed! " . mysql_error());

    while ($rs_ch = mysql_fetch_array($mysql_result_child))
    {
        $output .= "<option value='" . $rs_ch['Link'] . "'>" . $rs_ch['Name'] . "</option>";
    }

    $output .= "</optgroup>";
}

$output .= "</select>";

echo $output;

可以做一些进一步的改进:

  • 停止使用mysql_query并切换到更安全的 PDO ( http://php.net/manual/en/ref.pdo-mysql.php )。mysql_query无论如何,从 PHP 5.5.0 开始不推荐使用这些函数。
  • 重写您的查询以在单个查询中获取您需要的所有数据。这真的会加快速度。
  • 使用模板将 PHP 与 HTML 分开。
于 2013-07-04T21:58:58.583 回答