<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>

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

        How do I get the first level category to display only once?(如何让第一级类别只显示一次?)
        • <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.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                • <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>

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