从3个数据库表生成的三级嵌套列表

时间:2015-12-08 17:50:20

标签: php mysql

我正在尝试使用在3个单独的数据库表中找到的信息构建3级嵌套列表,这些表通过具有共同的外键和主键链接到其他数据库表。到目前为止,我已经想出如何生成列表的前两个级别,即主列表及其子列表,但是,我需要一些帮助来生成子列表的子列表。为了得到类似的东西:

<ul>
<li>List
<ul>
 <li>Sublist
   <ul>
    <li>
    Super_sublist
    </li>
   </ul>
  </li>
</ul>
</li>
<ul>

我使用以下代码生成列表的第一部分:

function get_menu($sql) {
    include 'connect.php';
    $result = $conn->query($sql);
    if ($result->num_rows > 0);
    $current_album = "";
    $level2 = "";

    echo "<div id='menu'>\n<div class='menu_pos'>\n<ul class='main-navigation'>\n"; 
    while ($row = $result->fetch_assoc()) {
        if ($current_album <> "" && $current_album <> $row['pageID']) {
        echo "</ul>\n</li>\n";
        }
        if ($current_album <> $row['pageID']) {
            echo "<li id='qoute'><a href='".$row['pageURL']."?pageID=".$row['pageID']."'>".$row['page_name']."</a>\n<ul>\n";  

        $current_album = $row['pageID'];
        }
        if ($row['GeneralID'] == NULL) {
            echo "";
        } else {        
        echo "<li><a href='#'>".$row['general_name']."</a>\n</li>";
        }

    }
    echo "</ul>\n</div>\n</div>\n";
}

结果是:

    <div id='menu'>
<div class='menu_pos'>
<ul class='main-navigation'>
<li id='qoute'><a href='0?pageID=7'>get quote</a>
<ul>
</ul>
</li>
<li id='qoute'><a href='info.php?pageID=4'>tjänster</a>
<ul>
<li><a href='#'>ovrigt stenarbete</a>
</li><li><a href='#'>lagning av trasiga stenskivor</a>
</li><li><a href='#'>montering av stenmaterial</a>
</li></ul>
</li>
<li id='qoute'><a href='info.php?pageID=3'>produkter</a>
<ul>
</li><li><a href='#'>golvplattor</a>
</li><li><a href='#'>köksbänkskivor</a>
</li><li><a href='#'>diskhoar</a>
</li><li><a href='#'>fönsterbänkar</a>
</li><li><a href='#'>golvplattor</a>
</li><li><a href='#'>fönsterbänkar</a>
</li></ul>
</li>
<li id='qoute'><a href='info.php?pageID=2'>om företaget</a>
<ul>
<li><a href='#'>utställning</a>
</li><li><a href='#'>köpvillkor</a>
</li></ul>
</li>
<li id='qoute'><a href='index.php?pageID=1'>hem</a>
<ul>
</ul>
</div>
</div>

使用以下sql查询生成此数据:

select page_list.pageID as pageID, page_list.page_name as page_name,
       page_list.pageURL as pageURL, level2_menu.GeneralID as GeneralID,
       level2_menu.gener_name as general_name, level3_menu.deepID as deepID,
       level3_menu.deep_title as deep_title 
FROM page_list 
   LEFT OUTER JOIN level2_menu on page_list.pageID = level2_menu.pageID 
   LEFT OUTER JOIN level3_menu on level2_menu.GeneralID = level3_menu.generalID 

UNION

select page_list.pageID as pageID, page_list.page_name as page_name,
       page_list.pageURL as pageURL, level2_menu.GeneralID as GeneralID,
       level2_menu.gener_name as general_name, level3_menu.deepID as deepID,
       level3_menu.deep_title as deep_title 
FROM page_list 
   RIGHT OUTER JOIN level2_menu on page_list.pageID = level2_menu.pageID     
   RIGHT  OUTER JOIN level3_menu on level2_menu.GeneralID = level3_menu.generalID 
order by pageID desc

这给出了以下sql结果:http://sqlfiddle.com/#!9/927a0

+--------------------+------------+----------+-----------+---------------+--------+------------+
| pageID Ascending 1 | page_name  | pageURL  | GeneralID | general_name  | deepID | deep_title |
+--------------------+------------+----------+-----------+---------------+--------+------------+
|                  7 | get quote  | 0        | NULL      | NULL          | NULL   | NULL       |
|                  4 | tjanster   | info.php | 9         | Title         | NULL   | NULL       |
|                  3 | products   | info.php | 5         | Title 2       | 8      | Subtitle   |
+--------------------+------------+----------+-----------+---------------+--------+------------+

1 个答案:

答案 0 :(得分:0)

我使用不同的方式使用php

<?php
				$table='page_list';
				$sql = "SELECT * FROM $table ORDER BY ID desc" ;  
				$result = mysqli_query($conn, $sql) or die('Erreur SQL !'.$req.'<br>'.mysqli_error($conn));
	while($row = $result->fetch_assoc()){

	$table2='Secondtable';
	$name=$row['NAME'];
	$sql2 = "SELECT * FROM $table2 WHERE Name='$name'";
	$result2 = mysqli_query($conn, $sql2) or die('Erreur SQL !'.$req.'<br>'.mysqli_error($conn));
	while($row2 = $result2->fetch_assoc()){  
	  	  ?>
	
html elements here using the $row and $row2

<?php ;
}
}
?>
当然你必须按照你想要的方式修改$ sql

你可以深入了解你想要的多个关卡,你不需要任何常见的外键和主键。 我希望这有帮助