如何创建mysql php多级列表导航

发布于 2024-12-09 08:14:49 字数 182 浏览 1 评论 0原文

基本上我希望能够创建一个多级导航(许多子导航)。显然,我知道这将通过创建彼此的列表来完成,但我非常坚持正确显示它的逻辑。

我看过有关父母/孩子关系的内容,但找不到任何有效且易于理解的内容。

我不需要知道 HTML 是如何构建的。 php/mysql 如何生成列表。

希望你能帮忙。

一个

Basically I want to be able to create a multi-level navigation (many sub navs). Obviously I know this will be done through creating lists with in each other but I am pretty stuck on the logic of displaying it correctly.

I have seen stuff regarding parent/children relationships but can't find anything that is efficient and easy to udnerstand.

I don't need to know how the HTML is built. Just how the php/mysql can generate the lists.

Hope you can help.

A

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

热情消退 2024-12-16 08:14:50

如果你指的是 HTML,它是这样的:

<ul>
   <li>
      <a href="#">Title</a>
      <ul>
         <li><a href="#">Title</a></li>
         <li><a href="#">Title</a></li>
         <li><a href="#">Title</a></li>
      </ul>
   </li>
   <li><a href="#">Title</a></li>
   <li><a href="#">Title</a></li>
   <li><a href="#">Title</a></li>
</ul>

If you mean the HTML it's like this:

<ul>
   <li>
      <a href="#">Title</a>
      <ul>
         <li><a href="#">Title</a></li>
         <li><a href="#">Title</a></li>
         <li><a href="#">Title</a></li>
      </ul>
   </li>
   <li><a href="#">Title</a></li>
   <li><a href="#">Title</a></li>
   <li><a href="#">Title</a></li>
</ul>
习惯成性 2024-12-16 08:14:50

假设你知道如何创建一个充满 mysql 内容的表
假设您有下表:Universes >类别>市场>段

1) 列出选择中“Universes”的内容。当用户选择时,调用另一个 .php 脚本并向其发送所选 Universe 的 id(使用 GET 或 POST)

2) 列出“类别”的内容,其中 idUniverses = 您发送到第二个脚本的 id。

3) 市场也是如此...

使用 AJAX 更容易。

需要代码吗?

assuming you know how to create filled with the content of a mysql table
assuming you have the following tables : Universes > Categories > Markets > Segments

1) list the content of 'Universes' in a select. when the user picks, call another .php script and send it the id of the chosen Universe (using GET or POST)

2) list the content of 'Categories', WHERE idUniverses = the id you sent to the second script.

3) same for the Markets...

It's easier with AJAX.

need the code ?

七月上 2024-12-16 08:14:49

这是我使用的代码。它构建具有无限级别子项的无序列表。

/*
* Table has 3 fields: `ID`, `PARENTID` and `NAME`
* `ID` is unique, `PARENTID` showing his parent node id.
* This function will go through it and build unordered list and call itself when needed to build subitems.
* $level argument used to define wich node's subitems to build. Default is 0 which is top level.
*/

function showMenu($level = 0) {

$result = mysql_query("SELECT * FROM `tbl_structure` WHERE `PARENTID` = ".$level); 
echo "<ul>";
    while ($node = mysql_fetch_array($result)) { 
        echo "<li>".$node['NAME'];
        $hasChild = mysql_fetch_array(mysql_query("SELECT * FROM `tbl_structure` WHERE `PARENTID` = ".$node['ID'])) != null;
        IF ($hasChild) {
            showMenu($node['ID']);
        }
        echo "</li>";
    }
echo "</ul>";
}

希望有帮助。

Here is code I used. It builds unordered list with unlimited level of subitems.

/*
* Table has 3 fields: `ID`, `PARENTID` and `NAME`
* `ID` is unique, `PARENTID` showing his parent node id.
* This function will go through it and build unordered list and call itself when needed to build subitems.
* $level argument used to define wich node's subitems to build. Default is 0 which is top level.
*/

function showMenu($level = 0) {

$result = mysql_query("SELECT * FROM `tbl_structure` WHERE `PARENTID` = ".$level); 
echo "<ul>";
    while ($node = mysql_fetch_array($result)) { 
        echo "<li>".$node['NAME'];
        $hasChild = mysql_fetch_array(mysql_query("SELECT * FROM `tbl_structure` WHERE `PARENTID` = ".$node['ID'])) != null;
        IF ($hasChild) {
            showMenu($node['ID']);
        }
        echo "</li>";
    }
echo "</ul>";
}

Hope that helps.

放肆 2024-12-16 08:14:49

我认为最有效的方法是从数据库中一次性获取所有记录,然后在 php 中再次构建层次结构。

因此,您的数据库中将具有与此类似的结构:

id    parent_id    menu_item

然后您可以获取所有项目并使用递归函数构建一个分层数组,您可以循环遍历该数组以获取菜单、子菜单、子子菜单等。 项目。请参阅此问题和关于如何重建结构的前两个答案

I think the most efficient would be to get all records in one go from the database and then build the hierarchical structure again in php.

So you would have a structure similar to this in your database:

id    parent_id    menu_item

Then you can get all items and use a recursive function to build a hierarchical array which you can loop through to get your menu, sub-menu, sub-sub-menu, etc. items. See this question and the top-two answers on how to re-build the structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文