gpt4 book ai didi

php - 使用mysql php在结构中显示类别和子类别

转载 作者:行者123 更新时间:2023-11-29 03:07:43 24 4
gpt4 key购买 nike

我在 php 和 mysql 中工作,我有一个表类别,我需要在父类别下显示子类别。喜欢

类别 1

  -> Subcat1

-> Subcat11

-> Subcat2

--> Subcat21

--> Subcat211

第二类

  -> Subcat2

-> Subcat21

-->Subcat22
-> Subcat3

--> Subcat31

--> Subcat311

我的表结构是这样的:

CREATE TABLE IF NOT EXISTS `category` (
`cat_id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT 'This is primary key of the table',
`name` varchar(255) DEFAULT NULL,
`parent_cat_id` bigint(11) NOT NULL,
PRIMARY KEY (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

你能帮帮我吗,如何执行我的查询。

最佳答案

例子:

<?php
class RightMenu {
public $menu;
public $patch = '';

public function getMenu($parent = 0) {
$DB = new DataBase();
$result = $DB->exec("SELECT * FROM cont_sections WHERE parentid = $parent AND status = 1 ORDER BY name;");
if(mysql_num_rows($result) > 0) {
echo '<ul>';
while($row = mysql_fetch_assoc($result)) {
if($row['parentid'] == 0){
$this->patch = '';
}
$this->patch .= '/' . $row["translit"];
echo '<li><a href="/section'.$this->patch.'.html">' . $row["name"] . '</a>';
echo $this->getMenu($row["id"]);
echo '</li>';
}
echo '</ul>';
}
mysql_free_result($result);
unset($DB);
}
}
?>

关于php - 使用mysql php在结构中显示类别和子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12893912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com