动态下拉菜单和子菜单 php mysqli with slug value
Dynamic Drop Down Menu and submenu php mysqli with slug value
我有一个需要从数据库动态创建的菜单。需要有菜单和子菜单
例如(我想要的是):
<li class="<?= ($pg == 'departments') ? 'active':''; ?>">
<a href="departments">Departments</a>
<ul class="dropdown">
<li>
<a href="department">CSE</a><br>
<a href="department">ECE</a>
<ul class="dropdown dropdown-right">
<li ><a href="department?slug=about-cse-department">About The Department</a></li>
<li ><a href="head-of-department?slug=about-cse-hod">HOD</a></li>
<li ><a href="department-vision-mission?slug=about-cse-vision-mision">Vision & Mission</a></li>
<li ><a href="department-facilities?slug=about-cse-department-facility">Department Facilities</a></li>
<li ><a href="department-goals?slug=about-cse-department-goals">Department Goals</a></li>
<?php } } ?>
</ul>
</li>
<?php } ?>
</ul>
</li>
主要部门和子菜单是 CSE、ECE、EEE、CIVIL,我在下拉菜单下方的每个子菜单都会出现,在 mysqli 中 table 我还将创建和存储数据和 slug names.And下拉菜单也会重复,请在下面的附件中找到。
我的答案是
部门-> CSE->关于部门
部门-> ECE->关于部门
部门-> CSE->HOD
部门-> ECE->HOD
每个菜单只有一个。
我的密码是
<li class="<?= ($pg == 'departments') ? 'active':''; ?>">
<a href="departments">Departments</a>
<ul class="dropdown">
<?php $sql2 ="SELECT * from `departments` ";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$department = $row2['dept_name']; ?>
<li>
<a href="department"><?=$department;?></a><!-- Department Names as shoen image-->
<ul class="dropdown dropdown-right">
<?php $sql3 ="SELECT * from `page` WHERE page_department = '$department' ";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc())
{
$pname = $row3['page_name'];
echo $slug = $row3['page_slug']; ?>
<li ><a href="department?slug=<?=$slug;?>">About The Department</a></li>
<li ><a href="head-of-department?slug=<?=$slug;?>">HOD</a></li>
<li ><a href="department-vision-mission?slug=<?=$slug;?>">Vision & Mission</a></li>
<li ><a href="department-facilities?slug=<?=$slug;?>">Department Facilities</a></li>
<li ><a href="department-goals?slug=<?=$slug;?>">Department Goals</a></li>
<li ><a href="department-faculty?slug=<?=$slug;?>">Faculty</a></li>
<li ><a href="department-publications?slug=<?=$slug;?>">Faculty Publications</a></li>
<li ><a href="department-syllabus?slug=<?=$slug;?>">Syllabus</a></li>
<li ><a href="department-workshops-seminars?slug=<?=$slug;?>">Workshops</a></li>
<li ><a href="department-student-toppers?slug=<?=$slug;?>">Student Toppers</a></li>
<?php } } ?>
</ul>
</li>
<?php } ?>
</ul>
</li>
在上面的代码中运行 like image can i upload this type will came.
我的 table 部门和页面 table 看起来像这样
you are repeating complete menu in while loop so its showing multiple time, as you already fetching sub menu by table then just create a single link and fetch related sub menu name and url with database but as i found you have different hrefs on your sub menu so you placed multiple links in submenu here you need to update your page table , you need to create a column as code below , hope this work:
<li class="<?= ($pg == 'departments') ? 'active':''; ?>">
<a href="departments">Departments</a>
<ul class="dropdown">
<?php $sql2 ="SELECT * from `departments` ";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$department = $row2['nit_dept_name']; ?>
<li>
<a href="department"><?=$department;?></a><!-- Department Names as shoen image-->
<ul class="dropdown dropdown-right">
<?php $sql3 ="SELECT * from `page` WHERE page_department = '$department' ";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc())
{
$pname = $row3['page_name'];
$slug = $row3['page_slug'];
$href= $row3['href']; //create a column in page table for href
?>
<li ><a href="<?php echo $href;?>?slug=<?=$slug;?>"><?php echo $pname;?></a></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
</li>
你的重复 while 循环一次检查这个
<ul class="dropdown">
<?php $sql2 ="SELECT * from `departments` ";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$department = $row2['nit_dept_name']; ?>
<li>
<a href="department"><?=$department;?></a><!-- Department Names as shoen image-->
<ul class="dropdown dropdown-right">
<?php $sql3 ="SELECT * from `page` WHERE page_department = '$department' ";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc())
{
$pname = $row3['page_name'];
$slug = $row3['page_slug'];
$href= $row3['href']; //create a column in page table for href
?>
<li ><a href="<?php echo $href;?>?slug=<?=$slug;?>"><?php echo $pname;?></a></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
我有一个需要从数据库动态创建的菜单。需要有菜单和子菜单
例如(我想要的是):
<li class="<?= ($pg == 'departments') ? 'active':''; ?>">
<a href="departments">Departments</a>
<ul class="dropdown">
<li>
<a href="department">CSE</a><br>
<a href="department">ECE</a>
<ul class="dropdown dropdown-right">
<li ><a href="department?slug=about-cse-department">About The Department</a></li>
<li ><a href="head-of-department?slug=about-cse-hod">HOD</a></li>
<li ><a href="department-vision-mission?slug=about-cse-vision-mision">Vision & Mission</a></li>
<li ><a href="department-facilities?slug=about-cse-department-facility">Department Facilities</a></li>
<li ><a href="department-goals?slug=about-cse-department-goals">Department Goals</a></li>
<?php } } ?>
</ul>
</li>
<?php } ?>
</ul>
</li>
主要部门和子菜单是 CSE、ECE、EEE、CIVIL,我在下拉菜单下方的每个子菜单都会出现,在 mysqli 中 table 我还将创建和存储数据和 slug names.And下拉菜单也会重复,请在下面的附件中找到。
我的答案是
部门-> CSE->关于部门 部门-> ECE->关于部门
部门-> CSE->HOD 部门-> ECE->HOD
每个菜单只有一个。
我的密码是
<li class="<?= ($pg == 'departments') ? 'active':''; ?>">
<a href="departments">Departments</a>
<ul class="dropdown">
<?php $sql2 ="SELECT * from `departments` ";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$department = $row2['dept_name']; ?>
<li>
<a href="department"><?=$department;?></a><!-- Department Names as shoen image-->
<ul class="dropdown dropdown-right">
<?php $sql3 ="SELECT * from `page` WHERE page_department = '$department' ";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc())
{
$pname = $row3['page_name'];
echo $slug = $row3['page_slug']; ?>
<li ><a href="department?slug=<?=$slug;?>">About The Department</a></li>
<li ><a href="head-of-department?slug=<?=$slug;?>">HOD</a></li>
<li ><a href="department-vision-mission?slug=<?=$slug;?>">Vision & Mission</a></li>
<li ><a href="department-facilities?slug=<?=$slug;?>">Department Facilities</a></li>
<li ><a href="department-goals?slug=<?=$slug;?>">Department Goals</a></li>
<li ><a href="department-faculty?slug=<?=$slug;?>">Faculty</a></li>
<li ><a href="department-publications?slug=<?=$slug;?>">Faculty Publications</a></li>
<li ><a href="department-syllabus?slug=<?=$slug;?>">Syllabus</a></li>
<li ><a href="department-workshops-seminars?slug=<?=$slug;?>">Workshops</a></li>
<li ><a href="department-student-toppers?slug=<?=$slug;?>">Student Toppers</a></li>
<?php } } ?>
</ul>
</li>
<?php } ?>
</ul>
</li>
在上面的代码中运行 like image can i upload this type will came.
我的 table 部门和页面 table 看起来像这样
you are repeating complete menu in while loop so its showing multiple time, as you already fetching sub menu by table then just create a single link and fetch related sub menu name and url with database but as i found you have different hrefs on your sub menu so you placed multiple links in submenu here you need to update your page table , you need to create a column as code below , hope this work:
<li class="<?= ($pg == 'departments') ? 'active':''; ?>">
<a href="departments">Departments</a>
<ul class="dropdown">
<?php $sql2 ="SELECT * from `departments` ";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$department = $row2['nit_dept_name']; ?>
<li>
<a href="department"><?=$department;?></a><!-- Department Names as shoen image-->
<ul class="dropdown dropdown-right">
<?php $sql3 ="SELECT * from `page` WHERE page_department = '$department' ";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc())
{
$pname = $row3['page_name'];
$slug = $row3['page_slug'];
$href= $row3['href']; //create a column in page table for href
?>
<li ><a href="<?php echo $href;?>?slug=<?=$slug;?>"><?php echo $pname;?></a></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
</li>
你的重复 while 循环一次检查这个
<ul class="dropdown">
<?php $sql2 ="SELECT * from `departments` ";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$department = $row2['nit_dept_name']; ?>
<li>
<a href="department"><?=$department;?></a><!-- Department Names as shoen image-->
<ul class="dropdown dropdown-right">
<?php $sql3 ="SELECT * from `page` WHERE page_department = '$department' ";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc())
{
$pname = $row3['page_name'];
$slug = $row3['page_slug'];
$href= $row3['href']; //create a column in page table for href
?>
<li ><a href="<?php echo $href;?>?slug=<?=$slug;?>"><?php echo $pname;?></a></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>