简体   繁体   English

如何创建MySQL PHP多级列表导航

[英]How to create a mysql php multi-level list navigation

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. 我不需要知道HTML是如何构建的。 Just how the php/mysql can generate the lists. 以及php / mysql如何生成列表。

Hope you can help. 希望能对您有所帮助。

A 一种

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. 希望能有所帮助。

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. 我认为最有效的方法是一次从数据库中获取所有记录,然后再次在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. 请参阅此问题以及有关如何重新构建结构的前两个答案

If you mean the HTML it's like this: 如果您指的是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>

assuming you know how to create filled with the content of a mysql table assuming you have the following tables : Universes > Categories > Markets > Segments 假设您知道如何创建充满mysql表内容的数据库,并假定您具有以下表:Universes>类别>市场>细分

1) list the content of 'Universes' in a select. 1)在选择中列出“宇宙”的内容。 when the user picks, call another .php script and send it the id of the chosen Universe (using GET or POST) 当用户选择时,调用另一个.php脚本并将其发送给所选Universe的ID(使用GET或POST)

2) list the content of 'Categories', WHERE idUniverses = the id you sent to the second script. 2)列出“类别”的内容,其中idUniverses =您发送给第二个脚本的ID。

3) same for the Markets... 3)与市场相同...

It's easier with AJAX. 使用AJAX更容易。

need the code ? 需要代码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM