1

我有两个名为“父母”和“孩子”的mysql表

在我父母的表中,我有 4 列(id、link、lable、have_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

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

有什么办法吗?

4

3 回答 3

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;
于 2013-08-25T05:01:50.990 回答
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);

产量:

蔬菜

  • 萝卜
  • 甜菜

水果

  • 苹果
于 2013-08-25T05:11:31.783 回答
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   
        }
        ?>
于 2013-08-25T05:14:50.013 回答