Add a class to <li> if sub menu exist using mysqli query - php

I want to know, how do i add a class to a <li> if it has a sub_menu?
I have a proper HTML menu populated from mysql database.
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li>About us
<ul class="dropdown-menu">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
</li>
<li class="dropdown">Contact</li>
</ul>
</div>
I am using bootstrap framework, so for the drop down menu i have to replace my <li> to this:
<li class='dropdown'>
<a aria-expanded='false' aria-haspopup='true' class='dropdown-toggle' data-toggle='dropdown' href='/about-us/' role='button'>
About us <span class='caret'></span>
</a>
</li>
I want to know how do i check if the <li> has any sub_menu before running a sub menu query in the loop? so that i can add markup for dropdown menu.
Here's the PHP Code that generates MENU:
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<?php
$res1 = $connection->query("SELECT * FROM primary_nav ORDER BY m_menu_id ASC");
while($menu=$res1->fetch_array())
{
?>
<li>
<a href="<?php echo $domain; ?><?php echo $menu['m_menu_link']; ?>">
<?php echo $menu['m_menu_name']; ?>
</a>
<?php
$res1_pro=$connection->query("SELECT * FROM primary_subnav WHERE m_menu_id=".$menu['m_menu_id']." ORDER BY seq ASC");
//echo "\xA";
?>
<?php
if(mysqli_num_rows($res1_pro) > 0) {
echo '<ul class="dropdown-menu">' ."\xA";
while ($pro1_row = $res1_pro->fetch_array()) {
?>
<li>
<a href="<?php echo $domain; ?><?php echo $pro1_row['s_menu_link']; ?>">
<?php echo $pro1_row['s_menu_name']; ?>
</a>
</li>
<?php
}
echo '</ul>' ."\xA";
}
?>
</li>
<?php
echo "\xA";
}
?>
</ul>
</div>
I could do thi with the jQuery, but i want to do it the HTML way.

You have to add the <li> with the class in the if check for the number of rows. Then you have to add an else clause to echo out the <li> without the class:
<?php
$res1 = $connection->query("SELECT * FROM primary_nav ORDER BY m_menu_id ASC");
while($menu=$res1->fetch_array())
{
$res1_pro=$connection->query("SELECT * FROM primary_subnav WHERE m_menu_id=".$menu['m_menu_id']." ORDER BY seq ASC");
//echo "\xA";
if(mysqli_num_rows($res1_pro) > 0) { // echo the list element with the class here
?>
<li class="foo">
<?php echo $menu['m_menu_name']; ?>
<?php
echo '<ul class="dropdown-menu">' ."\xA";
while ($pro1_row = $res1_pro->fetch_array()) {
?>
<li>
<?php echo $pro1_row['s_menu_name']; ?>
</li>
<?php
}
echo '</ul>' ."\xA";
} else { // echo the normal list element if number of rows is 0
?>
<li>
<?php echo $menu['m_menu_name']; ?>
<?php
}
?>
</li>
<?php
echo "\xA";
}
?>
Note: This particular coding style makes it very hard to write and maintain code should the need arise. You may want to look into ways to combine PHP and HTML to make it easier to update and maintain your code.

Related

bootstrap multilevel dropdown in navbar has unwanted empty space

I have bootstrap multilevel drop down navbar. when I click on the button I want the sub menu to drop right side of the list but it is overlapping last option. can anyone suggest me how to do it?
<ul class="nav navbar-nav col-lg-4">
<li class="active dropdown yamm-fw">
Home
</li>
<li class="dropdown"><a class="dropdown-toggle" data-hover="dropdown"
href="#"> category <span class="caret"></span> </a>
<ul class="dropdown-menu dropright">
<?php $sql=mysqli_query($con,"select id,categoryName from category
limit 10");
while($row=mysqli_fetch_array($sql))
{
$category_name=$row['categoryName'];
if ( $category_name == "Clothing")
{ ?>
<li class="dropdown yamm btn-group">
<a class="dropdown-toggle " data-toggle="dropdown" href="#"> <?php echo
$category_name; ?><span class="caret"></span></a>
<ul class="dropdown-menu dropright">
<?php $sql1=mysqli_query($con,"select id,subcategory from subcategory1
where categoryid=" .$row['id']);
while($row1=mysqli_fetch_array($sql1))
{ ?>
<li class="dropdown yamm">
<a href="category.php?cid=<?php echo $row1['id'];?>"> <?php echo
$row1['subcategory'];?></a>
</li>
<?php } ?>
</ul>
</li>
<?php }
else
{ ?>
<li class="dropdown yamm">
<a href="category.php?cid=<?php echo $row['id'];?>"> <?php echo
$row['categoryName'];?></a>
</li>
<?php
}
} ?>
</ul>
You may overcome this problem by writing the CSS the value of paddings.
.nav li {padding-top: 5px; padding-bottom: 5px;}
Also, have look at line-height property it might be create some problem. on some cases.

how to view submenu on click using php,html

I'm trying to view submenu after click on parent.
Classes->Primary School->(Grade1,Grade2,Grade3,Grade4,Grade5,Grade6)
Classes->Middle School->(Grade7,Grade8,Grade9)
Classes->Secondary School->(Grade10,Grade11,Grade12)
<div class="collapse navbar-collapse" id="main-navigation">
<ul class="nav navbar-nav navbar-right">
<?php foreach ($data as $menu) { ?>
<?php if(!$menu->children) { ?>
<li><?php echo $menu->name; ?></li>
<?php }
else {
?>
<li class="dropdown open">
<a href="#" class="dropdown-toggle " data-toggle="dropdown">
<?php echo $menu->name; ?>
<span class="fa fa-angle-down"></span>
</a>
<ul class="dropdown-menu open" role="menu">
<?php
foreach ($menu->children as $child) {
?>
<li><?php echo $child->name; ?></li>
<?php
foreach ($child->children as $sub_child) {
?>
<li class="dropdown-submenu">
<?php echo $sub_child->name; ?><span class="fa fa-angle-down"></span>
<ul class="dropdown-submenu" role="menu">
<?php } ?>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
</li>
</ul>
</div>
The result:
Problem with result
I want to view Grade 1 to Grade 6 after click on Primary school and from Grade 7 to Grade 9 after click on Middle school ...
Please help!
You have an open <ul class="dropdown-submenu" role="menu"> tag without any <li> or <a>'s being generated inside of it. Then you close two each statements before closing the <ul> tag.
<?php foreach ($child->children as $sub_child) { ?>
<li class="dropdown-submenu">
<a href="#" style="color:black;" class="dropdown-toggle " data-toggle="dropdown">
<?php echo $sub_child->name; ?><span class="fa fa-angle-down"></span>
</a>
<ul class="dropdown-submenu" role="menu">
<?php } ?>
<?php } ?>
</ul>
</li>
This will be causing all sorts of unintended markup output. Fix that and it should fix your problem. If not, you'll atleast be a lot closer.

How To Display In A List Using PHP

How do you repeatedly display the ID numbers in PHP then in a list?
This is the code I have in View
<ul class="dropdown-menu" role="menu">
<li><?php echo ?></li>
</ul>
In Controller
$query=$this->mdl_mainmenu->notif_items();
$data['notifs'] = $query;
$this->load->view('vw_main_menu',$data);
In Model
function notif_items(){
$user = $this->session->userdata("username");
$this->csbrms->select("deleted_user_request.sdp_no,
deleted_user_request.approval_status");
$this->csbrms->from('deleted_user_request');
$this->csbrms->join('m_employee_masters', 'm_employee_masters.emp_no = user_request.emp_no');
$this->csbrms->where('deleted_user_request.created_by',$user);
$this->csbrms->where('deleted_user_request.approval_status' , 'Closed');
$this->csbrms->where('deleted_user_request.approval_status' , 'Resolved');
$query = $this->csbrms->get();
return $query->result();
}
changed ul tag to :
<ul class="dropdown-menu" role="menu">
<?php foreach($vw_main_menu['notifs'] as $row):?>
<li>
<?php echo $row['deleted_user_request.sdp_no']?>
</li>
<?php endforeach;?>
</ul>
<ul class="dropdown-menu" role="menu">
<?php foreach($notifs as $row):?>
<li>
<?php echo $row->deleted_user_request.sdp_no; ?>
</li>
<?php endforeach;?>
</ul>

trying to get foreach loop to display items with subitems correctly in list items

I am really trying to get this to work and am close:
It loops through the array and does create the submenus (with an error albeit) but:
the first menu with submenu items appears correctly but on the second menu that has a submenu items it repeats the first submenu items and then the second submenu items..... what am I missing?
Any help would be greatly appreciated.
<ul class="nav navbar-nav">
<?php
$html = new cacheHTML('topmenu');
if(!$html->isCached){
$menuitems = getMenuLevelsArray(25, 1 , 1);
$submenuI = "0";
foreach($menuitems as $item){
if($item['submenu']){
$subs[$submenuI] = $item['submenu'];
}
?>
<li <?php if($subs[$submenuI]){ ?> class="dropdown" <?php }?>>
</span> <?php }?>
<!-- submenu begins here -->
<?php foreach($subs as $submenuI => $menu){ ?>
<ul class="dropdown-menu" role="menu">
<li>
<?php
for($a=0; $a < count($menu); $a++){
?>
<a href="<?php echo $menu[$a]['url']; ?>" <?php if(!$menu[$a+1]){ echo "class='last'"; } ?>><?php echo $menu[$a]['text']; ?></a>
<?php } ?>
</li>
</ul>
<?php $submenuI++; } ?>
</li>
<?php
}
?>
<?php } $html->show(); ?>
</ul>
`<ul class="nav navbar-nav">
<li> Home <br>
<b>Warning</b>: Invalid argument supplied for foreach() in <b>/layout.php</b> on line 95<br>
</li>
<li class="dropdown open">
About<span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>
About
Board of Directors
Structure
</li>
</ul>
</li>
<li>
Standards & Codes
<ul class="dropdown-menu" role="menu">
<li>
About
Board of Directors
Structure
</li>
</ul>
</li>
<li class="dropdown">
Resources <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>
About
Board of Directors
Structure
</li>
</ul>
<ul class="dropdown-menu" role="menu">
<li>
Frequently Asked Questions
News Archives
Resource Links
Safety Alerts
</li>
</ul>
</li>
<li>Contact Us
<ul class="dropdown-menu" role="menu">
<li>
About
Board of Directors Structure
</li>
</ul>
<ul class="dropdown-menu" role="menu">
<li>
Frequently Asked Questions
News Archives
Resource Links
Safety Alerts
</li>
</ul>
</li>
</ul>`
Under the first "foreach" loop you should reset your $subs array to make sure it's empty. Otherwise it gets down to the second loop, sees the old "$subs" from the previous loop, and loops through it again:
foreach ( $menuitems as $item ) {
$subs = array();
if ($item ['submenu']) {
$subs [$submenuI] = $item ['submenu'];
}
...
I found the mixture of PHP and HTML hard to read, so I rewrote the code to make it easier for my eyes:
$html = new cacheHTML ( 'topmenu' );
if (! $html->isCached) {
$menuitems = getMenuLevelsArray ( 25, 1, 1 );
$submenuI = "0";
foreach ( $menuitems as $item ) {
if ($item ['submenu']) {
$subs [$submenuI] = $item ['submenu'];
}
echo '<li';
if($subs[$submenuI]){
echo 'class="dropdown"';
}
echo ">
<a href='{$item['url']}'";
if($subs[$submenuI]){
echo 'class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"';
}
echo ">{$item['text']}";
if($subs[$submenuI]){
echo '<span class="caret"></span>';
}
echo '</a> <!-- submenu begins here -->
';
foreach($subs as $submenuI => $menu){
echo '<ul class="dropdown-menu" role="menu">
<li>
';
for($a = 0; $a < count ( $menu ); $a ++) {
echo "<a href='{$menu[$a]['url']}'";
if(!$menu[$a+1]){
echo "class='last'";
}
echo ">{$menu[$a]['text']}</a>
";
}
echo '</li>
</ul>
';
$submenuI++;
}
echo '</li>';
}
}
$html->show(); ?>
</ul>
I hope this helps!
Finally got it...
I need to learn me some more PHP if statements and foreach's
here is what I did:
<ul class="nav navbar-nav">
<?php
$html = new cacheHTML('topmenu');
if(!$html->isCached){
$menuitems = getMenuLevelsArray(25, 1 , 1);
$submenuI = "0";
foreach($menuitems as $item){
if($item['submenu']){
$subs[$submenuI] = $item['submenu'];
}
?>
<li <?php if($subs[$submenuI]){ ?> class="dropdown" <?php }?>>
</span> <?php }?>
<!-- submenu begins here -->
<?php foreach($subs as $submenuI => $menu){ ?>
<ul class="dropdown-menu" role="menu">
<li>
<?php
for($a=0; $a < count($menu); $a++){
?>
<a href="<?php echo $menu[$a]['url']; ?>" <?php if(!$menu[$a+1]){ echo "class='last'"; } ?>><?php echo $menu[$a]['text']; ?></a>
<?php } ?>
</li>
</ul>
<?php $submenuI++; } ?>
</li>
<?php
}
?>
<?php } $html->show(); ?>
</ul>

Generating divs in php?

Say I have the following div:
<div id="nav">
<ul>
<li class="navButton">Home</li>
<li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar
Bodies</a></li>
<li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar
Pickguards</a></li>
<li class="navButton">Contact Us</li>
</ul>
</div>
I would want this to be repeated a certain number of times so the resulting html would look like:
<div id="nav">
<ul>
<li class="navButton">Home</li>
<li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar
Bodies</a></li>
<li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar
Pickguards</a></li>
<li class="navButton">Contact Us</li>
</ul>
</div> <div id="nav">
<ul>
<li class="navButton">Home</li>
<li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar
Bodies</a></li>
<li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar
Pickguards</a></li>
<li class="navButton">Contact Us</li>
</ul>
</div> <div id="nav">
<ul>
<li class="navButton">Home</li>
<li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar
Bodies</a></li>
<li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar
Pickguards</a></li>
<li class="navButton">Contact Us</li>
</ul>
</div>
If the number was 3. What php command is used to do this sort of thing? (I know I use a for loop, but I mean the code in between)
Essentially what I will eventually have is something like this:
for each product
div product
text from product file
close div product
Thanks
Just use the foreach or while like this:
foreach($array as $value){
?>
<div>
whatever <?php other php here ?>
</div>
<?php
}
<div class='nav'>
<ul>
<?php foreach ($products as $product): ?>
<li class='navbutton'><?php echo $product->text; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
if you have multiple different divs, make a foreach loop for that too, if you want same div to repeat 3 times, wrap it around a for loop
<?
for ($results as $value) {
echo '<div id="nav">';
echo ' <ul>';
echo ' <li class="navButton">Home</li>';
echo ' <li class="navButton">'.$value['name1'].'</li>';
echo ' <li class="navButton">'.$value['name2'].'</li>';
echo ' <li class="navButton">Contact Us</li>';
echo ' </ul>';
echo ' </div>';
}
?>

Categories