Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从数据库及其子类别中获取菜单_Php_Html_Mysql_Yii_Menu - Fatal编程技术网

Php 如何从数据库及其子类别中获取菜单

Php 如何从数据库及其子类别中获取菜单,php,html,mysql,yii,menu,Php,Html,Mysql,Yii,Menu,这是我的菜单 <ul class="sub-menu"> <li><a href="product-category.html">Product 1</a></li> <li><a href="product-category.html">Product 2</a> <

这是我的菜单

              <ul class="sub-menu">
                <li><a href="product-category.html">Product 1</a></li>
                <li><a href="product-category.html">Product 2</a>
                  <ul class="sub-menu">
                    <li><a href="product-category.html">P 21</a></li>
                    <li><a href="product-category.html">P 22</a></li>
                    <li><a href="product-category.html">P 23</a></li>
                    <li><a href="product-category.html">P 24</a></li>
                  </ul>
                </li>
                <li><a href="product-category.html">Product 3</a></li>
                <li><a href="product-category.html">Product 4</a></li>
              </ul>
Product1、Product2、Product3是类别表中的类别。 P21、P22、P23、P24是子类别中产品2的子类别。 所有类别都没有子类别。 类别id保存在子类别表中

如果不存在,则创建表
类别
(
cid
int(11)非空自动增量,
cname
varchar(255)不为空, 主键(
cid
) )ENGINE=InnoDB默认字符集=latin1自动增量=1

如果不存在,则创建表
子类别
(
sid
int(11)非空自动增量,
sname
varchar(255)不为空,
cid
int(11)不为空, 主键(
sid
) )ENGINE=InnoDB默认字符集=latin1自动增量=1


如何通过一次查询从MySql中查找数据来在菜单中显示它?

请向我展示数据库的体系结构。一般来说,它需要2个周期,有一个条件,第一个是类别,第二个是子类别。 file.php


唯一的问题是输入正确条件的条件需要知道数据库的一个标准。

您可以通过替换表名和所有特定细节来使用此查询

SELECT * FROM `parenttable` p WHERE p.foreignkey IN (SELECT c.foreignkey FROM `childtable` c WHERE c.foreignkey = p.foreignkey)

在事先不知道数据库结构的情况下帮助使用
SQL
是很困难的。这将使所有字段都与多对多相关。您是否需要创建3个表:1.表类别和列:cat_id,name等2.表子类别开关列:subcat_id,name等3.cat和subcat的中介表:id,cat_id,subcat_id,您是否检查中介表是否存在某人的子类别cat id$query=“select*from Intermediar where cat_id=$category['cat_id'”$result=mysql\u num\u行($result)//如果为空,则cat no have SUBACT$result是“显示或不显示子类别”的条件。您需要在数据库中使用中介表作为evite多对多关系
SELECT * FROM `parenttable` p WHERE p.foreignkey IN (SELECT c.foreignkey FROM `childtable` c WHERE c.foreignkey = p.foreignkey)