简体   繁体   English

如何使第一级类别仅显示一次?

[英]How do I get the first level category to display only once?

I have a table set up in mysql as follows: 我在mysql中设置了一个表,如下所示:

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: 我试图让它们显示在树状结构中,如下所示:

A Night Out 晚上出去

  • Clubs and Societies 社团与社团
  • Public houses 公共房屋
  • Restaurants 餐馆
  • Taxis and Private Hire Vehicles 出租车和私人交通工具
  • Theatres and concert Halls 剧院和音乐厅
  • Wine Bars 红酒吧

Accommodation 住所

  • Bed and Breakfast 民宿
  • Guest Houses 待客室
  • Holiday Accommodation 假日住宿
  • Hotels 酒店
  • Self Catering Accommodation 自炊式住宿

Agriculture 农业

  • Agricultural Machinery and Spares 农业机械及配件
  • Agricultural Merchants 农业商人
  • Animal Feedstuffs 动物饲料
  • Country Wear 乡村服饰
  • Dairies 乳业

etc 等等

At the moment I am able to create the following: 目前,我可以创建以下内容:

using this code: 使用此代码:

$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>";
}

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. 我认为我需要使用foreach,但不确定。

The third level category table I have set up has the same structure as this table but is subsubcategory_id / subsubcategory_parent / subsubcategory_name. 我设置的第三级类别表具有与该表相同的结构,但为subsubcategory_id / subsubcategory_parent / subsubcategory_name。

only output the catname when it changes. 仅在更改catname时输出。 You will also need to order your query by category_name first. 您还需要先按category_name排序查询。

$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>";

Edit: 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. 更好的方法是使用为此目的而制作的嵌套集

I think you would need something along the lines of this: 我认为您将需要一些类似的方法:

$cat_query = mysql_query("SELECT * FROM `TABLE_NAME` GROUP BY `subcategory_parent`") or die(mysql_error());

while($r_cat = mysql_fetch_array($cat_query)) {

    $cat_name = $r_cat['subcategory_parent'];

    echo("$cat_name<br />");

    $sub_cat_query = mysql_query("SELECT * FROM `subcategory_name` WHERE (`subcategory_parent` = '$cat_name')") or die(mysql_error());

    while($r_sub_cat = mysql_fetch_array($sub_cat_query)) {

        $sub_cat_name = $r_sub_cat['subcategory_name'];

        echo(" - $sub_cat_name<br />");

    }

    echo("<br />");

}

There is probably a more efficient way of doing this, a lot of the guys here are far more knowledgeable in this field, but this should do the trick! 可能有一种更有效的方式来执行此操作,这里的许多人对此领域的知识更丰富,但这应该可以解决问题! You may also want to check, table names/column titles etc too, to make sure they match your database. 您可能还需要检查表名/列标题等,以确保它们与您的数据库匹配。

I'd simplify the data structure if you want the possibility of many-layered sub-categories (which you've said you might). 如果您想使用多层子类别(您曾说过可能),那么我将简化数据结构。 The best way I've found to do something like this is to do something slightly different with data: 我发现做这种事情的最好方法是对数据做一些稍微不同的事情:

category
+----+-----------+------------+----------------------------+
| id | parent_id | sort_order | name                       |
+----+-----------+------------+----------------------------+
| 1  | 0         | 0          | A Night Out                |
| 2  | 1         | 1          | Clubs and Societies        |
| 3  | 1         | 2          | Public houses              |
| 4  | 1         | 3          | Restaurants                |
| 5  | 1         | 4          | Taxis ...                  |
| 6  | 1         | 5          | Theatres ...               |
| 7  | 1         | 6          | Wine Bars                  |
| 8  | 0         | 0          | Accommodation              |
| 9  | 8         | 1          | Bed and Breakfast          |
| 10 | 8         | 2          | Guest Houses               |
| x  | x         | x          | ... and so on ...          |
+----+-----------+------------+----------------------------+

Note: I'm only using the sort_order to sort on child nodes with the same parent. 注意:我使用sort_order对具有相同父级的子节点进行排序。

Now I'd create a really simple SQL query to just retrieve the data ordered by parent_id then sort_order : 现在,我将创建一个非常简单的SQL查询,以仅检索由parent_id然后sort_order排序的数据:

SELECT category.id, category.parent_id, category.name FROM category ORDER BY category.parent_id ASC, category.sort_order ASC

Now comes the "fun" part ... because, really, what we want here is structured data so I'd pass the query into an XML DOMDocument() to create a structured category system. 现在出现“乐趣”部分了……因为,实际上,我们想要的是结构化数据,因此我将查询传递到XML DOMDocument()中以创建结构化类别系统。

Essentially you loop through the data and as you're doing that loop you create nodes with an id something like node_{$category_id} and you can then assign the child nodes to their parent in the right order. 本质上,您遍历数据,并且在执行此循环时,会创建一个具有类似node_{$category_id}的id的node_{$category_id} ,然后可以按正确的顺序将子节点分配给它们的父节点。 You can then use standard DOM navigation to retrieve the data tree you want. 然后,您可以使用标准DOM导航来检索所需的数据树。

The biggest advantage with this is that you can start from any child node and walk back up the node tree to create your category list - which means you can have it expand and collapse. 这样做的最大好处是,您可以从任何子节点开始,然后返回节点树以创建类别列表-这意味着您可以使其展开和折叠。

I did write a far more detailed explanation recently; 我最近确实写了更详细的解释。 I can post a link to it if you'd like. 如果您愿意,我可以发布一个链接。

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

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