如何显示类别和子类别?
如何显示类别和子类别? 我在数据库中有一张表。该表中的行看起来像这样:
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`parent_id` int(11) NOT NULL,
`order` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
我想要像此网站上那样显示类别和子类别: http://www.dealsdirect.com.au/c/baby-1/(左侧菜单)
我的尝试:
<?php
include 'Category_model.php';
include 'Advert_model.php';
$nr = $_GET['id'];
function show_category($nr){
try
{
$pdo = new PDO('mysql:host=localhost;dbname=advert', 'root', '');
$pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo -> query("SELECT * FROM category where `parent_id` = $nr");
echo "<ul>";
foreach ($stmt as $row){
echo "<li><a href=category_view.php?id={$row['id']}> {$row['name']}</a>".show_category($row['id'])."</li>";
}
echo "</ul>";
//$id = $_GET['id'];
}
catch(PDOException $e)
{
echo 'Error!: ' . $e->getMessage();
}
}
show_category($nr);
How to display category and subcategory ?
I have one table in DB. Row in this table looks something like this:
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`parent_id` int(11) NOT NULL,
`order` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
I want display category and subcategory like on this site: http://www.dealsdirect.com.au/c/baby-1/ (menu on the left)
My try:
<?php
include 'Category_model.php';
include 'Advert_model.php';
$nr = $_GET['id'];
function show_category($nr){
try
{
$pdo = new PDO('mysql:host=localhost;dbname=advert', 'root', '');
$pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo -> query("SELECT * FROM category where `parent_id` = $nr");
echo "<ul>";
foreach ($stmt as $row){
echo "<li><a href=category_view.php?id={$row['id']}> {$row['name']}</a>".show_category($row['id'])."</li>";
}
echo "</ul>";
//$id = $_GET['id'];
}
catch(PDOException $e)
{
echo 'Error!: ' . $e->getMessage();
}
}
show_category($nr);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面的代码应该可以帮助您入门。它是一个“递归函数”——一个调用自身的函数。正如@Fake51 刚才提到的,它的效率不是很高,但它应该可以工作。
您需要一些 CSS 来构建您生成的列表。
编辑:由于仍然没有被接受的答案,这里是我的代码修改为使用单个 SQL 查询来完成这一切,这应该更有效,尽管可能更令人困惑。看看你的情况如何。
The below code should get you started. It's a "recursive function" - a function that calls itself. As @Fake51 has just mentioned, it's not very efficient, but it should work.
You'll need some CSS to structure the list you've produced.
Edit: Since there's still been no accepted answer, here's my code modified to do it all with a single SQL query which should be much more efficient, albeit a little more confusing potentially. See how it goes for you.
对于这样的结构,您需要首先为所选项目构建查询,然后为所选项目父级构建查询。本质上,您需要对您拥有的每个级别的菜单进行查询,这些菜单应该是可见的。
这就是树关系的parent_id方案效率不高的原因。最好使用嵌套集(http://en.wikipedia.org/wiki/Nested_set_model)或物化路径(http://en.wikipedia.org/wiki/Materialized_path)。如果您希望对项目进行一些更新,从长远来看,物化路径可能会更容易处理 - 我个人更喜欢它,因为已经尝试了两者。
With a structure like that, you'll need to first construct a query for the chosen item, then afterwards construct a query for the chosen items parent. Essentially, you'll need a query for each level of menus you have, that should be visible.
This is the reason why a parent_id scheme of tree relationship is not very efficient. Much better to go with nested set (http://en.wikipedia.org/wiki/Nested_set_model) or materialized path (http://en.wikipedia.org/wiki/Materialized_path). If you expect to do some updating of the items, materialized path will probably be easier to deal with in the long run - I personally much prefer it, having tried both.
由于您只需要显示类别及其所有子类别,因此不需要递归来构建菜单,并且您当然不希望在数据库调用中递归。所以最基本的解决方案(所有代码都在一个文件中)将是这样的:
Since you only need category and all its subcategories displayed you don't need recursion for menu construction, and you certainly don't want recursion in your DB calls. So most basic solution (all code in one file) would be something like this: