Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类别层次结构(PHP/MySQL)_Php_Mysql_Hierarchy_Categories - Fatal编程技术网

类别层次结构(PHP/MySQL)

类别层次结构(PHP/MySQL),php,mysql,hierarchy,categories,Php,Mysql,Hierarchy,Categories,我正在尝试从MySQL数据库中获取层次结构中的所有类别和子类别: 我的结果应该是这样的(仅举一个例子): A类 子类别1 Sub_Sub_第1类 Sub_Sub_第2类 Sub_第2类 B类 C类 MySQL代码: CREATE TABLE IF NOT EXISTS `categories` ( `category_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `parent_id` mediumint(8) uns

我正在尝试从MySQL数据库中获取层次结构中的所有类别和子类别:

我的结果应该是这样的(仅举一个例子):

  • A类
    • 子类别1
      • Sub_Sub_第1类
      • Sub_Sub_第2类
    • Sub_第2类
  • B类
  • C类
  • MySQL代码:

    CREATE TABLE IF NOT EXISTS `categories` (
       `category_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
       `parent_id` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT 'for sub-categories'
      PRIMARY KEY (`category_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ;
    

    简单地说,如何使用PHP代码在Hirarch中获得它?

    当使用邻接列表模型时,您可以一次生成结构

    摘自:

    从链接的文章中,这里有一个创建输出列表的片段。它是递归的,如果一个节点有子节点,它会再次调用自己来建立子树

    function toUL(array $array)
    {
        $html = '<ul>' . PHP_EOL;
    
        foreach ($array as $value)
        {
            $html .= '<li>' . $value['name'];
            if (!empty($value['children']))
            {
                $html .= toUL($value['children']);
            }
            $html .= '</li>' . PHP_EOL;
        }
    
        $html .= '</ul>' . PHP_EOL;
    
        return $html;
    }
    
    函数toUL(数组$array)
    {
    $html='
      '.PHP\u EOL; foreach($array作为$value) { $html.='
    • '.$value['name']; 如果(!empty($value['children'])) { $html.=toUL($value['children']); } $html.='
    • '.PHP\u EOL; } $html.='
    '.PHP\u EOL; 返回$html; }
    相关问题:


    还有另一种方法可以达到同样的效果,我发现这更容易遵循(没有参考技巧)。通过将相关信息添加到当前节点及其父节点来构建树(假设foreach迭代SQL查询返回的行):

    并显示树:

    function toUL($tree, $id, $html){
      $html .= '<ul>'.PHP_EOL;
    
      if (isset($tree[$id]['name']))
        $html .= '<li>' . $tree[$id]['name'];
    
      if (isset($tree[$id]['children']))
      {
        $arChildren = &$tree[$id]['children'];
        $len = count($arChildren);
        for ($i=0; $i<$len; $i++) {
            $html .= toUL($tree, $arChildren[$i], "");
        }
        $html .= '</li>'.PHP_EOL;
      }
    
      $html .= '</ul>'.PHP_EOL;
      return $html;
    }
    
    // Display the tree
    echo toUL($tree, 0, "");
    
    函数toUL($tree,$id,$html){
    $html.='
      '.PHP\u EOL; 如果(isset($tree[$id]['name'])) $html.='
    • '.$tree[$id]['name']; if(isset($tree[$id]['children'])) { $arChildren=&$tree[$id]['children']; $len=计数($arChildren);
      对于($i=0;$i@Amnon),您的代码工作得非常完美。只需使用CodeIgniter对其进行测试,它的工作效果非常出色。如果有人需要,下面是工作代码:

      <?php
      
      function disTree($all_cats) {
      $tree = array();
      foreach ($all_cats as $cat)
      {
          $pid  = $cat->parent_id;
          $id   = $cat->cat_id;
          $name = $cat->cat_name;
      
          // Create or add child information to the parent node
          if (isset($tree[$pid]))
              // a node for the parent exists
              // add another child id to this parent
              $tree[$pid]["children"][] = $id;
          else
              // create the first child to this parent
              $tree[$pid] = array("children"=>array($id));
      
          // Create or add name information for current node
          if (isset($tree[$id]))
              // a node for the id exists:
              // set the name of current node
              $tree[$id]["name"] = $name;
          else
              // create the current node and give it a name
              $tree[$id] = array( "name"=>$name );
      }
      return $tree;
      }
      
      
      function toUL($tree, $id, $html){
        $html .= '<ul>'.PHP_EOL;
      
        if (isset($tree[$id]['name']))
          $html .= '<li>' . $tree[$id]['name'];
      
        if (isset($tree[$id]['children']))
        {
          $arChildren = &$tree[$id]['children'];
          $len = count($arChildren);
          for ($i=0; $i<$len; $i++) {
              $html .= toUL($tree, $arChildren[$i], "");
          }
          $html .= '</li>'.PHP_EOL;
        }
      
        $html .= '</ul>'.PHP_EOL;
        return $html;
      }
      
      $tree = disTree($all_cats);
      // Display the tree
      echo toUL($tree, 0, "");
      
      ?>
      
      
      

      我唯一改变的是添加了我自己的数组($all_cats)。

      我有一个新想法,我认为它会很好。 这个想法是: 在category_parent列中,我们将插入对此节点的所有父节点的引用

      +----+----------------------+-----------------+ | id | category_name | hierarchy | +----+----------------------+-----------------+ | 1 | cat1 | 1 | +----+----------------------+-----------------+ | 2 | cat2 | 2 | +----+----------------------+-----------------+ | 3 | cat3 | 3 | +----+----------------------+-----------------+ | 4 | subcat1_1 | 1-4 | +----+----------------------+-----------------+ | 5 | subcat1_2 | 1-5 | +----+----------------------+-----------------+ | 6 | subsubcat1_1 | 1-4-6 | +----+----------------------+-----------------+ | 7 | subsubcat1_2 | 1-4-7 | +----+----------------------+-----------------+ | 8 | subsubcat1_3 | 1-4-8 | +----+----------------------+-----------------+ | 9 | subsubcat1_3_1 | 1-4-8-9 | +----+----------------------+-----------------+ | 10 | subsubcat1_3_2 | 1-4-8-10 | +----+----------------------+-----------------+ | 11 | subsubcat1_3_1_1 | 1-4-8-9-11 | +----+----------------------+-----------------+ | 12 | subsubsubcat1_3_1_1 | 1-4-8-9-12 | +----+----------------------+-----------------+ | 13 | subsubsubcat1_3_1_2 | 1-4-8-9-11-13 | +----+----------------------+-----------------+ | 14 | subsubsubcat1_2_1_3 | 1-4-8-9-11-14 | +----+----------------------+-----------------+ 现在让我们进行您想要的查询:

      1-车辆的所有子类别:

      select * from table_name where hierarchy like '1-%'
      
      2-如果您需要黑色的所有父项,只需键入:

      select * from table_name where hierarchy = '1-4-8-9' or hierarchy = '1-4-8' or hierarchy = '1-4' or hierarchy = '1'
      
      (您可以从php构建该查询,在'-'字符处拆分层次结构字段)

      3-要查看所有类别,包括级别和直接父级:

      select *, SUBSTR(hierarchy, 1, (LENGTH(hierarchy) - LENGTH(id) - 1)) as parent, LENGTH(hierarchy) - LENGTH(REPLACE(hierarchy, '-', '')) as level From table_name
      
      +----+----------------------+-----------------+-----------+--------+ |id |类别名称|层次结构|父级|级别| +----+----------------------+-----------------+-----------+--------+ |1 | cat1 | 1 | 0| +----+----------------------+-----------------+-----------+--------+ |第二类| +----+----------------------+-----------------+-----------+--------+ |3 | cat3 | 3 | 0| +----+----------------------+-----------------+-----------+--------+ |4 |子1 | 1-4 | 1 | 1| +----+----------------------+-----------------+-----------+--------+ |5 |副1 | 2 | 1-5 | 1 | 1| +----+----------------------+-----------------+-----------+--------+ |6 |子分类1 | 1-4-6 | 1-4 | 2| +----+----------------------+-----------------+-----------+--------+ |7 |子分类1 | 2 | 1-4-7 | 1-4 | 2| +----+----------------------+-----------------+-----------+--------+ |8 |子分类1 | 3 | 1-4-8 | 1-4 | 2| +----+----------------------+-----------------+-----------+--------+ |9 |子分类1 | 3 | 1-4-8-9 | 1-4-8 | 3| +----+----------------------+-----------------+-----------+--------+ |10 |子分类1 | u 3 | u 2 | 1-4-8-10 | 1-4-8 | 3| +----+----------------------+-----------------+-----------+--------+ |11 |子分类1 | 3 | 1 | 1-4-8-9-11 | 1-4-8-9 | 4| +----+----------------------+-----------------+-----------+--------+ |12 |子分包1 | 3 | 1 | 1-4-8-9-12 | 1-4-8-9 | 4| +----+----------------------+-----------------+-----------+--------+ |13 |子分包1 | u 3 | u 1 | u 2 | 1-4-8-9-11-13 | 1-4-8-9-11 | 5| +----+----------------------+-----------------+-----------+--------+ |14 |子分包1 | u 2 | u 1 | u 3 | 1-4-8-9-11-14 | 1-4-8-9-11 | 5| +----+----------------------+-----------------+-----------+--------+
      这是一个新的想法,需要一些改进。

      尝试以下代码

      //连接到mysql并选择db

      $conn=mysqli_connect('localhost','user','password','database');

      如果(!empty($conn->connect_errno))死亡(“Error”.mysqli_Error($conn));
      //调用递归函数打印类别列表
      类别树(0);
      //递归php函数
      功能类别树($catid){
      全球$conn;
      $sql=“从父类中选择*,父类id=””$catid。“”;
      $result=$conn->query($sql);
      而($row=mysqli\u fetch\u object($result)):
      $i=0;
      如果($i==0)回显“
        ”; 回显“
      • ”.$row->cat_名称; 类别树($row->id); 回音“
      • ”; $i++; 如果($i>0)回显“
      ”; 结束时; } //关闭连接 mysqli_close($conn); ?>


      +1令人惊讶的是,第一部分完全满足了我的需要,而且没有我想象的那么复杂。这是我意识到的一个非常聪明的想法。我很想将这个数组转换为JSON数组,但没有ID作为索引。这也可能吗?我在已经动态创建的菜单中使用它时遇到了问题。我需要t删除第一个生成的ul。我通过引用开始和结束ul来修复它,并关闭其中的toUL。例如:$html.='
        '.toUL($v['children'])。
      ';什么应该作为数组传递给toUL()方法???$refs或$list或$row?但是,如果您必须
      select * from table_name where hierarchy like '1-%'
      
      select * from table_name where hierarchy = '1-4-8-9' or hierarchy = '1-4-8' or hierarchy = '1-4' or hierarchy = '1'
      
      select *, SUBSTR(hierarchy, 1, (LENGTH(hierarchy) - LENGTH(id) - 1)) as parent, LENGTH(hierarchy) - LENGTH(REPLACE(hierarchy, '-', '')) as level From table_name
      
      +----+----------------------+-----------------+-----------+--------+ | id | category name | hierarchy | parent | level | +----+----------------------+-----------------+-----------+--------+ | 1 | cat1 | 1 | | 0 | +----+----------------------+-----------------+-----------+--------+ | 2 | cat2 | 2 | | 0 | +----+----------------------+-----------------+-----------+--------+ | 3 | cat3 | 3 | | 0 | +----+----------------------+-----------------+-----------+--------+ | 4 | subcat1_1 | 1-4 | 1 | 1 | +----+----------------------+-----------------+-----------+--------+ | 5 | subcat1_2 | 1-5 | 1 | 1 | +----+----------------------+-----------------+-----------+--------+ | 6 | subsubcat1_1 | 1-4-6 | 1-4 | 2 | +----+----------------------+-----------------+-----------+--------+ | 7 | subsubcat1_2 | 1-4-7 | 1-4 | 2 | +----+----------------------+-----------------+-----------+--------+ | 8 | subsubcat1_3 | 1-4-8 | 1-4 | 2 | +----+----------------------+-----------------+-----------+--------+ | 9 | subsubcat1_3_1 | 1-4-8-9 | 1-4-8 | 3 | +----+----------------------+-----------------+-----------+--------+ | 10 | subsubcat1_3_2 | 1-4-8-10 | 1-4-8 | 3 | +----+----------------------+-----------------+-----------+--------+ | 11 | subsubcat1_3_1_1 | 1-4-8-9-11 | 1-4-8-9 | 4 | +----+----------------------+-----------------+-----------+--------+ | 12 | subsubsubcat1_3_1_1 | 1-4-8-9-12 | 1-4-8-9 | 4 | +----+----------------------+-----------------+-----------+--------+ | 13 | subsubsubcat1_3_1_2 | 1-4-8-9-11-13 |1-4-8-9-11 | 5 | +----+----------------------+-----------------+-----------+--------+ | 14 | subsubsubcat1_2_1_3 | 1-4-8-9-11-14 |1-4-8-9-11 | 5 | +----+----------------------+-----------------+-----------+--------+
      if( !empty($conn->connect_errno)) die("Error " . mysqli_error($conn));
      
      //call the recursive function to print category listing
      category_tree(0);
      
      //Recursive php function
      function category_tree($catid){
      global $conn;
      
      $sql = "select * from category where parent_id ='".$catid."'";
      $result = $conn->query($sql);
      
      while($row = mysqli_fetch_object($result)):
      $i = 0;
      if ($i == 0) echo '<ul>';
       echo '<li>' . $row->cat_name;
       category_tree($row->id);
       echo '</li>';
      $i++;
       if ($i > 0) echo '</ul>';
      endwhile;
      }
      //close the connection
      mysqli_close($conn);
      ?>