使用两个mysql表创建动态菜单

时间:2013-08-25 04:36:43

标签: php mysql menu

我有两个名为“parents”和“childs”的mysql表

在我的父母表中,我有4列(id,link,lable,have_childs)

在我的childs表中我也有4列(id,c_link,c_lable,parent_id)

我使用像这样的查询获取值

"SELECT parents.*, childs.* FROM parents, childs WHERE parents.id = childs.p_id;"

然后使用foreach循环我得到了这个结果

array(7) { ["id"]=> string(1) "1" ["link"]=> string(3) "veg" ["lable"]=> string(9) "Vegitable" ["childs"]=> string(1) "1" ["c_link"]=> string(6) "carrot" ["c_lable"]=> string(6) "carrot" ["p_id"]=> string(1) "1" } 
array(7) { ["id"]=> string(1) "2" ["link"]=> string(3) "Fru" ["lable"]=> string(6) "Fruits" ["childs"]=> string(1) "1" ["c_link"]=> string(6) "grapes" ["c_lable"]=> string(6) "grapes" ["p_id"]=> string(1) "2" } 
array(7) { ["id"]=> string(1) "3" ["link"]=> string(3) "veg" ["lable"]=> string(9) "Vegitable" ["childs"]=> string(1) "1" ["c_link"]=> string(5) "beeat" ["c_lable"]=> string(5) "beeat" ["p_id"]=> string(1) "1" } 

然后我做了这个

<?php
foreach($result as $myresult){ ?>
    <ul>
    <li><a href="<?php echo $myresult['link']; ?>"><?php echo $myresult['lable']; ?></a>
        <?php 
            if($myresult['childs'] == 1){
                echo '<div><ul>';
                echo '<li><a href="'.$myresult['c_link'].'">'.$myresult['c_lable'].'</a></li>';
                echo '</div></ul>';
            }
        ?>

<?php   
}
?>

然后我得到了这个结果

.Vegitable
   carrot
.Fruits
   grapes
.Vegitable
   beet

但这不是我要找的结果 我需要胡萝卜和甜菜都是蔬菜。

有没有办法做到这一点?

3 个答案:

答案 0 :(得分:2)

我创建了以下表格。

<强>父母

+----+------+-----------+
| id | link | label     |
+----+------+-----------+
|  1 | veg  | Vegetable |
|  2 | fru  | Fruit     |
+----+------+-----------+

<强>儿童的

+----+-----------+--------+---------+
| id | parent_id | link   | label   |
+----+-----------+--------+---------+
|  1 |         1 | beets  | Beets   |
|  2 |         1 | carrot | carrots |
|  3 |         2 | apple  | Apples  |
+----+-----------+--------+---------+

这样做的一种方法是选择所有父母,并循环每个父母获得他们的孩子。这是一些伪代码。我可能搞乱了ul / li的嵌套,但你应该明白这个想法。

// Start by getting all the parents.
$parents = // SELECT * FROM PARENTS;

// Loop through all the parents.
@foreach ($parents as $parent) {
    echo "<ul>";
    echo "<li>" . $parent['label'];

    // Get all the childs of the current parent.
    $childs = // SELECT * FROM CHILDS WHERE parent_id = $parent['id'];

    // Loop through all the childs of the current parent.
    @if ($childs has results) {
        echo "<ul">;

        @foreach ($childs as $child) {
            echo "<li>" . $child['label'] . "</li>";
        @endforeach

        echo "</ul>";
        echo "</li>";
    @else
        echo "</li>";
    @endif

    echo "</ul">;

@endforeach;

答案 1 :(得分:0)

以下是我所谈论的一个例子:

// Your results set emulated.
$dbResults = array(
    array(
        "id" => 1,
        "link" => "veg",
        "label" => "Vegitable",
        "childs" => 1,
        "c_link" => "carrot",
        "c_label" => "carrot",
        "p_id" => 1
    ),
    array(
        "id" => 2,
        "link" => "fru",
        "label" => "Fruit",
        "childs" => 1,
        "c_link" => "Apple",
        "c_label" => "Apple",
        "p_id" => 2
    ),
    array(
        "id" => 3,
        "link" => "veg",
        "label" => "Vegitable",
        "childs" => 1,
        "c_link" => "beet",
        "c_label" => "beet",
        "p_id" => 1
    )
     );

// Your actual menu, built from the above result set.
$menu = array();

// A simple illustration of building up your menu
foreach ($dbResults as $menuItem) {
    // For clarity
    $menuIndex = $menuItem['label'];
    $menu[$menuIndex][] = $menuItem['c_label'];
}

$output = '<ul>';
foreach ($menu as $parentLabel => $children) {
    $output .= "<li>{$parentLabel}</li>";

    if (!empty($children)) {
        $output .= "<ul>";
        foreach ($children as $childLabel) {
            $output .= "<li>{$childLabel}</li>";     
        }
        $output .= "</ul>";
    }   
}
$output .= "</ul>";

print($output);

收率:

Vegitable

  • 胡萝卜
  • 甜菜

水果

  • 苹果

答案 2 :(得分:0)

如果您的结果按标签排序,则

$lastLable="";
foreach($result as $myresult){ 
$lable=$myresult['lable']
?>
<ul>
    <li>
    <?php
    if ($lable!=$lastLable) 
    {
    ?>
    <a href="<?php echo $myresult['link']; ?>"><?php echo $myresult['lable']; ?></a>
        <?php 
        }
        if($myresult['childs'] == 1){
        echo '<div><ul>';
        echo '<li><a href="'.$myresult['c_link'].'">'.$myresult['c_lable'].'</a></li>';
        echo '</div></ul>';
        echo "</li>";
        $lastLable=$lable;
        }
        ?>

        <?php   
        }
        ?>