在<option> PHP </option>中显示子菜单

时间:2014-10-12 15:55:08

标签: php sql xml

我的菜单首先有两个表是main_menu,第二个是sub_menu,在我的主菜单中我有两个字段id和catname,在子菜单表中我有三个字段paret_id和parentname和iden(自动增量)。

在这张照片中,我加入了表格: query result http://upload7.ir/imgs/2014-10/64888235604214323477.png

我希望在<select>标记中显示此查询结果(针对每个catname),例如:

<select>
    <BR><BR>
    <optgroup=catname>
        <BR>
        <option value='iden'>
            parentname
        </option>
        <BR>
    </optgroup>
    <BR><BR>
</select><BR>

我尝试将整个查询结果放在数组

$query = $db->query("SELECT mainmenu.id ,mainmenu.catname , submenu.parent_id , submenu.parentname , submenu.iden FROM mainmenu INNER JOIN submenu ON mainmenu.id = submenu.parent_id");

if($query->rowCount() >= 1){

    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
        $rows [] = $row;
         $submenu = array_column($rows,'catname');
    }
     $catname = array_unique($submenu);
    <select>
    <?php
        foreach ($catname as $cat) {
            echo "<optgroup label=$cat>";
                foreach ($rows as $row) {
                    echo "<option>" .$row['parentname']."</option>";

                }
            echo "</optgroup>";
        }

    ?>

</select>
}

但我不知道怎么做!!请帮我。 我想在一个查询中执行此操作,这对我来说非常重要

2 个答案:

答案 0 :(得分:0)

谢谢:)我修复了这里是我的代码

$query = $db->query("SELECT mainmenu.id ,mainmenu.catname , submenu.parent_id , submenu.parentname , submenu.iden FROM mainmenu INNER JOIN submenu ON mainmenu.id = submenu.parent_id");

if($query->rowCount() >= 1){

    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
        $rows [] = $row;
    }
    echo '<pre>',print_r($rows),'</pre>';
    $parent_id = array_column($rows,'parent_id');
    $submenu = array_column($rows,'catname');
}
 $catname = array_unique($submenu);

foreach ($catname as $c) {
     $cc [] = $c;
 }
 print_r($cc);
?>




<select>
    <?php 
     $i= 1;
    foreach ($cc as $res) {           
        echo "<optgroup label=$res>";
        foreach ($rows as $row) {
            if($row['parent_id'] == $i){
                echo "<option value=".$row['iden'].">".$row['parentname']. "</option>";
            }
        }
    echo "</optgroup>";
    $i++;
    }

    ?>
</select>

答案 1 :(得分:0)

它完成了..

$query = $db->query("SELECT mainmenu.id ,mainmenu.catname , submenu.parent_id , submenu.parentname , submenu.iden FROM mainmenu INNER JOIN submenu ON mainmenu.id = submenu.parent_id");

$menu = array(); 
if($query->rowCount() >= 1){ 

    #Created a custom multidimensional array
    while (list( $cat_id, $catname, $parent_id, $parentname, $iden) = $query -> fetch(PDO::FETCH_NUM)) { 
        $menu[$catname][$iden] = $parentname; # The values $cat_id and $parent_id is not used
    }

    #Created the <select> list with <optgroup> from the Custom multidimensional array
    echo '<select>';
    foreach ($menu as $k1 => $v1){ 
        echo '<optgroup label = "'. $k1 .'">'; 
            foreach ($v1 as $k2  => $v2){ 
                echo '<option value = "' . $k2 . '">'. $v2 . '</option>';
            }
        echo '</optgroup>';
    }
    echo '</select>'; 
}

下次,尝试只选择SQL Query所需的值,以避免头痛。