<small id='dvOeQ'></small><noframes id='dvOeQ'>

  • <i id='dvOeQ'><tr id='dvOeQ'><dt id='dvOeQ'><q id='dvOeQ'><span id='dvOeQ'><b id='dvOeQ'><form id='dvOeQ'><ins id='dvOeQ'></ins><ul id='dvOeQ'></ul><sub id='dvOeQ'></sub></form><legend id='dvOeQ'></legend><bdo id='dvOeQ'><pre id='dvOeQ'><center id='dvOeQ'></center></pre></bdo></b><th id='dvOeQ'></th></span></q></dt></tr></i><div id='dvOeQ'><tfoot id='dvOeQ'></tfoot><dl id='dvOeQ'><fieldset id='dvOeQ'></fieldset></dl></div>
  • <legend id='dvOeQ'><style id='dvOeQ'><dir id='dvOeQ'><q id='dvOeQ'></q></dir></style></legend>

      <bdo id='dvOeQ'></bdo><ul id='dvOeQ'></ul>
  • <tfoot id='dvOeQ'></tfoot>

        如何让第一级类别只显示一次?

        时间:2023-11-30
        • <i id='TOgPi'><tr id='TOgPi'><dt id='TOgPi'><q id='TOgPi'><span id='TOgPi'><b id='TOgPi'><form id='TOgPi'><ins id='TOgPi'></ins><ul id='TOgPi'></ul><sub id='TOgPi'></sub></form><legend id='TOgPi'></legend><bdo id='TOgPi'><pre id='TOgPi'><center id='TOgPi'></center></pre></bdo></b><th id='TOgPi'></th></span></q></dt></tr></i><div id='TOgPi'><tfoot id='TOgPi'></tfoot><dl id='TOgPi'><fieldset id='TOgPi'></fieldset></dl></div>

              <bdo id='TOgPi'></bdo><ul id='TOgPi'></ul>
              <tfoot id='TOgPi'></tfoot>

              <legend id='TOgPi'><style id='TOgPi'><dir id='TOgPi'><q id='TOgPi'></q></dir></style></legend>

                  <tbody id='TOgPi'></tbody>

                <small id='TOgPi'></small><noframes id='TOgPi'>

                • 本文介绍了如何让第一级类别只显示一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在mysql中设置了一个表,如下:

                  I have a table set up in mysql as follows:

                  我想创建一个侧边栏类别导航,其中包含父类别 - 子类别 - 以及我存储在另一个表中的可能的第三级类别.

                  I would like to create a sidebar category navigation with Parent category - subcategories - and possible third level categories which I have stored in another table.

                  我试图让它们显示在这样的树结构中:

                  I am trying to get them to display in a tree structure like so:

                  一夜情

                  • 俱乐部和社团
                  • 公共房屋
                  • 餐厅
                  • 出租车和私人租用车辆
                  • 剧院和音乐厅
                  • 酒吧

                  住宿

                  • 民宿
                  • 旅馆
                  • 度假住宿
                  • 酒店
                  • 自助式住宿

                  农业

                  • 农业机械及备件
                  • 农商行
                  • 动物饲料
                  • 乡村服饰
                  • 乳制品

                  目前我可以创建以下内容:

                  At the moment I am able to create the following:

                  使用此代码:

                  $dbc = mysql_connect($db_host,$db_user,$db_pass);
                  $sdb = mysql_select_db($db_database);
                  
                  $query = 'SELECT category_name, subcategory_name FROM categories, subcategories WHERE subcategory_parent = category_name';
                  
                  $result = mysql_query($query, $dbc)
                  or die (mysql_error($dbc));
                  
                  while($row = mysql_fetch_array($result)) {
                  
                  $catname = $row["category_name"];
                  $subcatname = $row["subcategory_name"];
                  
                  echo "<li>$catname</li><ul><li>$subcatname</li></ul>";
                  }
                  

                  我想让第一级类别只显示一次,子类别在它们下面的列表中.谁能告诉我最有效的方法吗?我想我需要使用 foreach 但不确定.

                  I want to get the first level category to display only once with the subcategories in a list below them. Can anyone tell me the most efficient way to do this? I think I need to use foreach but am not sure.

                  我建立的第三级类别表和这个表结构一样,但是是subsubcategory_id/subsubcategory_parent/subsubcategory_name.

                  The third level category table I have set up has the same structure as this table but is subsubcategory_id / subsubcategory_parent / subsubcategory_name.

                  推荐答案

                  只在catname变化时输出.您还需要先按 category_name 对查询进行排序.

                  only output the catname when it changes. You will also need to order your query by category_name first.

                  $row = mysql_fetch_array($result);
                  $catname = $row["category_name"];
                  $subcatname = $row["subcategory_name"];
                  $last = $catname;
                  
                  echo "<li>$catname</li><ul>"
                  
                  while($row = mysql_fetch_array($result)) {
                      $catname = $row["category_name"];
                      $subcatname = $row["subcategory_name"];
                      if($last != $catname){
                          echo "</ul><li>$catname</li><ul>"
                      }
                      echo "<li>$subcatname</li>";
                      $last = $catname;
                  }
                  echo "</ul>";
                  

                  只需重新完整阅读问题并想说,当涉及到分层树时,使用父/子(或类别/子/子子)不是最好的方法.虽然它确实有效,但它通常需要多个查询和递归函数才能显示.更好的方法是使用专门为此目的而制作的 嵌套集.

                  Just re-read the question fully and want to say that when it comes to hierarchical trees, using a parent/child (or category/sub/subsub) is not the best method. Although it does work, it usually requires multiple queries and recursive functions for display. A better approach is to use the nested set which is made for exactly this purpose.

                  这篇关于如何让第一级类别只显示一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:php从多维数组动态创建导航菜单 下一篇:在 PHP 中使用 URL 突出显示当前导航选项卡

                  相关文章

                • <small id='7zuWQ'></small><noframes id='7zuWQ'>

                  <legend id='7zuWQ'><style id='7zuWQ'><dir id='7zuWQ'><q id='7zuWQ'></q></dir></style></legend>

                  <tfoot id='7zuWQ'></tfoot>
                  <i id='7zuWQ'><tr id='7zuWQ'><dt id='7zuWQ'><q id='7zuWQ'><span id='7zuWQ'><b id='7zuWQ'><form id='7zuWQ'><ins id='7zuWQ'></ins><ul id='7zuWQ'></ul><sub id='7zuWQ'></sub></form><legend id='7zuWQ'></legend><bdo id='7zuWQ'><pre id='7zuWQ'><center id='7zuWQ'></center></pre></bdo></b><th id='7zuWQ'></th></span></q></dt></tr></i><div id='7zuWQ'><tfoot id='7zuWQ'></tfoot><dl id='7zuWQ'><fieldset id='7zuWQ'></fieldset></dl></div>

                      • <bdo id='7zuWQ'></bdo><ul id='7zuWQ'></ul>