简体   繁体   English

如何创建没有父级ID的嵌套多级菜单

[英]How to create a nested multi level menu without parent id

I searched on many posts, but I can't find one to fix my problem. 我搜索了很多帖子,但找不到解决该问题的帖子。

In my database I have some data : a name and a "treepath" like "1.1.2". 在我的数据库中,我有一些数据:一个名称和一个“ treepath”,例如“ 1.1.2”。 I don't have a parent ID or anything like that. 我没有父母身份证或类似的东西。 So my question is : is there a way to createa nested multi level menu with only the "treepath" ? 所以我的问题是:有没有办法只用“ treepath”创建一个嵌套的多级菜单?

In my table I got (ID | NAME | TREEPATH): 在我的表格中,我得到了(ID | NAME | TREEPATH):

  • 0 | 0 | Phone | 电话| 1 1个
  • 1 | 1 | Samsung | 三星| 1.1 1.1
  • 2 | 2 | Galaxy S | Galaxy S | 1.1.1 1.1.1
  • 3 | 3 | Galaxy Note | Galaxy Note | 1.1.2 1.1.2

... ...

Output I need : 我需要的输出:

<ul>
  <li><a href="">1. Phones</a>
  <ul>
      <li><a href="">1.1. Samsung</a>
      <ul>
         <li><a href="">1.1.1. Galaxy S</a></li>
         <li><a href="">1.1.2. Galaxy Note</a></li>
         <li><a href="">1.1.3. Galaxy Ace</a></li>
       </ul></li>
        <li><a href="">1.2. Apple</a></li>
        <li><a href="">1.3. Google</a></li>
    </ul></li>
</ul>

I certainly need a recursive function with php, but i can't got it right, so if anyone could help me on this one, that'd be great ! 我当然需要使用php的递归函数,但是我做错了,所以如果有人可以在这方面帮助我,那真是太好了!

Thanks ! 谢谢 !

Well, you could use a recursive function that employs regular expressions, but that might be sub-optimal. 好吧,您可以使用采用正则表达式的递归函数,但是它可能不是最优的。 Pseudo-code follows: 伪代码如下:

function drawTree($root) {
  while($results = yourQuery("SELECT * FROM table WHERE treepath REGEXP '^{$root}\.'")) {
    echo "stuff";
    drawTree($results['treepath']);
  }  
}

Your situation is odd. 你的情况很奇怪。

I don't see a technical reason not to just have a parent id in your table. 我不认为在表中不仅仅具有父ID的技术原因。 Or to use first normal form. 或使用第一个范式。 Feels a bit contrived, but you could do something like: 感觉有些人为,但是您可以执行以下操作:

<?php
$db = new mysqli(...);
$stmnt = $db->prepare("select name, treepath, LENGTH(treepath) - LENGTH(REPLACE(treepath, '.', '')) AS depth from tree order by treepath");
$stmnt->bind_result($name,$treepath,depth);
$tree = array();
$current_depth = 0;
while($stmnt->fetch())
{
  if($current_depth < $depth)
  {
    echo '<ol><li>' . $name . '</li>';
    $current_depth=$depth;
  }
  else if($current_depth == $depth)
  {
    echo '<li>' . $name . '</li>';
  }
  else //current_depth greater than depth
  {
    echo '</ol><li>' . $name .'</li>';
  }
}
$stmnt->close();
$db->close();
echo '</ol>';
?>

Not tested, will need tweeks. 未经测试,将需要tweeks。

If your application needs to provide nested menus, you are better off implementing nesting ( In database language, it would be grouping, foreign keys and constraints ) in your database. 如果您的应用程序需要提供嵌套菜单,那么最好在数据库中实现嵌套(用数据库语言,它是分组,外键和约束)。 It's not recommended that you maintain different designs in your presentation layer and database layer. 不建议您在表示层和数据库层中维护不同的设计。 After all, a strong database design is pivotal for any application, isn't it? 毕竟,强大的数据库设计对于任何应用程序都是至关重要的,不是吗?

There are many other advantages as well - you streamline your application design, data remains organized and consistent, lesser effort in transforming database content into presentation content, and it becomes easier to add new features as well. 此外,还有许多其他优点-您可以简化应用程序设计,数据保持井井有条,一致,将数据库内容转换为演示内容的工作更少,并且添加新功能也变得更加容易。 Why don't you start with some categorization and a good schema design? 为什么不从某种分类和良好的模式设计入手呢?

If you ask me, I would make something like a " phones " table that has phone_name, year, manufacturer, model, type etc. Again, there would be a different table called manufacturers which have manufacturer id's, names, years , and other information. 如果您问我,我将创建一个类似“ phones ”的表,其中包含phone_name,year,制造商,型号,类型等。同样,将有一个名为manufacturers的不同表,其中包含manufacturer id's, names, years和其他信息。 。 You can also have another table for " type ". 您也可以为“ type ”创建另一个表。 There will be foreign key relationships between manufacturer id's and manufacturer column in phone table and similar stuff with other columns/tables as well. 电话表中的manufacturer id's and manufacturer列与其他列/表的相似内容之间也将具有foreign key relationships If you are confused, a good place to start off would be drawing an ER diagram which will eventually be put down into tables and views. 如果您感到困惑,那么一个好的起点应该是绘制一个ER图,该图最终将放到表和视图中。

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

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