mysql数据库中的可点击菜单

时间:2017-12-03 05:12:05

标签: php mysql

我需要从mysql数据库创建一个菜单。

CREATE TABLE category(
        id INT AUTO_INCREMENT PRIMARY KEY,
        product VARCHAR(20) NOT NULL,
        parent_id INT DEFAULT NULL
);

INSERT INTO category VALUES
(1,'ELECTRONICS',0),
(2,'TELEVISIONS',1),
(3,'COMPUTER',1),
(4,'DELL',3),
(5,'LCD',2),
(6,'PLASMA',2),
(7,'FLASH',6),
(8,'BIKE',0),
(9,'MOTORCYCLE',8),
(10,'SCOOTER',8),
(11,'BAJAJ',9),
(12,'MAHINDRA',10);

SQLFiddle

    <div>
       <form id="my_form1" action="" method="post" >
<?php 

$sql = mysqli_query($connection, "SELECT 
    t1.product AS lev1
    FROM category AS t1
    LEFT JOIN category AS t2 ON t2.parent_id = t1.id
    LEFT JOIN category AS t3 ON t3.parent_id = t2.id
    LEFT JOIN category AS t4 ON t4.parent_id = t3.id
    WHERE t1.parent_id = '0' group by t1.id");

    while($sql_result = mysqli_fetch_assoc($sql)){
                            ?>

<a name="aqwse" href="#" onclick=""><?php echo $sql_result['lev1']; ?><br></a>
    <?php
    }
    ?>  
</form>
    </div>

通过上面的查询,我只能获得第一级。我将如何获得我在sqlfiddle的sql查询中显示的第二级,第三级或第四级。假设我在ELECTRONICS上顺序点击&gt;&gt;电视&gt;&gt; PLASMA&gt;&gt;闪。我没能创建php表单。

0 个答案:

没有答案