我在数据库中有3个表,我想加入这些表以构建3级嵌套列表(将用作菜单的一部分)。到目前为止,我已经想出如何从下表中的表中选择数据。我希望在php部分生成菜单的第3级有一些帮助。:
SQL表:
page_list:pageID(primary_key)page_name pageURL
level2_menu :GeneralID(primary_key)gener_name pageID(foreign_key);
level3_menu:deepID(primary_key)generalID(foreign_key)deep_title
使用:
<?php
get_menu("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级列表和2级列表的数据外,还可以使用:
function get_menu($sql)
{
include 'connect.php';
$result = $conn->query($sql);
if ($result->num_rows > 0)
;
$current_album = "";
echo "<div id='menu'><div class='menu_pos'><ul class='main-navigation'>\n";
while ($row = $result->fetch_assoc()) {
if ($current_album <> "" && $current_album <> $row['pageID'])
echo "</ul></li>";
if ($current_album <> $row['pageID']) {
echo "<li id='qoute'><a href='#'>" . $row['page_name'] . "</a><ul>\n";
$current_album = $row['pageID'];
}
if ($row['GeneralID'] == 'NULL') {
echo "bob";
} else {
echo "<li><a href='#'>" . $row['general_name'] . "</a></li>\n";
}
}
echo "</ul></div></div>";
}
这导致The first menu example shown here这是一个2级列表,但是我想要3级列表,就像第二个菜单示例一样,显示在jsfiddle中。
答案 0 :(得分:0)
首先,这是一种艰难的做事方式。原始SQL / RAW HTML / RAW PHP。模板化(例如twig),DB助手(例如Doctrine)可以制造魔法。
但要回答你的问题 - 这可能有用:
首先:我建议修改SQL
Ben@Ben-AMMS MINGW64 /c/xampp/htdocs/testAlaKloon
$ git clone https://github.com/Diseasedfire/AlaBonteKoe.git
Cloning into 'AlaBonteKoe'...
remote: Counting objects: 3027, done.
remote: Compressing objects: 100% (66/66), done.
remote: Total 3027 (delta 27), reused 0 (delta 0), pack-reused 2955
Receiving objects: 100% (3027/3027), 37.23 MiB | 588.00 KiB/s, done.
Resolving deltas: 100% (494/494), done.
Checking connectivity... done.
Ben@Ben-AMMS MINGW64 /c/xampp/htdocs/testAlaKloon
$ vim README.MD
(I make edits to the readme)
Ben@Ben-AMMS MINGW64 /c/xampp/htdocs/testAlaKloon/AlaBonteKoe (master)
$ git commit -a -m "test remote commit"
[master 318b667] test remote commit
1 file changed, 1 insertion(+)
Ben@Ben-AMMS MINGW64 /c/xampp/htdocs/testAlaKloon/AlaBonteKoe (master)
$ git remote add origin https://github.com/nebbii/AlaBonteKoe.git
fatal: remote origin already exists.
Ben@Ben-AMMS MINGW64 /c/xampp/htdocs/testAlaKloon/AlaBonteKoe (master)
$ git remote -v
origin https://github.com/Diseasedfire/AlaBonteKoe.git (fetch)
origin https://github.com/Diseasedfire/AlaBonteKoe.git (push)
Ben@Ben-AMMS MINGW64 /c/xampp/htdocs/testAlaKloon/AlaBonteKoe (master)
$ git push origin master
remote: Permission to Diseasedfire/AlaBonteKoe.git denied to nebbii.
fatal: unable to access 'https://github.com/Diseasedfire/AlaBonteKoe.git/':
The requested URL returned error: 403
第二:分割准备菜单和输出菜单。
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
ORDER BY pageID DESC, GeneralID DESC, deepID DESC