How To Display In A List Using PHP - 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>

Related

How to add class on each item using while loop

I just want to add a class on each items using the while loop in php. But the problem is it will add only to the item that is greater than 2, not to all items. My goal is to add class "nav-multiple" on all items if the items is exceed to 2.
<ul>
<?php
$index = -1;
// loop through the rows of data
while ( have_rows('property_section_menu_repeater_field') ) : the_row();
$index++;
$title = get_sub_field('property_section_menu_repeater_subfield1');
$unique_id = get_sub_field('property_section_menu_repeater_subfield2');
?>
<li class="nav-item <?php echo $index >= 2 ? 'nav-multiple' : ''; ?>">
<a class="nav-link p-0" href="#<?php echo $unique_id; ?>"><?php echo $title; ?></a>
</li>
<?php endwhile; ?>
</ul>
Current output:
<li class="nav-item">
<a></a>
</li>
<li class="nav-item nav-multiple">
<a></a>
</li>
The output that i want to achieve:
<li class="nav-item nav-multiple">
<a></a>
</li>
<li class="nav-item nav-multiple">
<a></a>
</li>
Okay I solved it now by using php count() function and foreach loop
<ul>
<?php
$rows = get_field('property_section_menu_repeater_field');
// loop through the rows of data
foreach($rows as $key => $row):
$title = $row['property_section_menu_repeater_subfield1'];
$unique_id = $row['property_section_menu_repeater_subfield2'];
?>
<li class="nav-item <?php echo count($rows) >= 2 ? 'nav-item-multiple' : ''; ?>">
<a class="nav-link p-0" href="#<?php echo $unique_id; ?>"><?php echo $title; ?></a>
</li>
<?php endforeach; ?>
</ul>

where and while loop not seem to be working

I want to select some values using 'where' and 'and' clause in bootstrap. The code selects only one value meanwhile there are more than one in the database. Here goes the code:
<ul class="collapsible collapsible-accordion">
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i>ENGLISH LANGUAGE<i class="fa fa-angle-down rotate-icon"></i></a>
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE subject = 'ENGLISH LANGUAGE' AND class = 'JHS 2'";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
while($row=$result->fetch()){
?>
<div class="collapsible-body">
<ul class="list-unstyled">
<?php echo '<li>'?><a data-id="<?php echo $key['note_id']?>" href="<?php echo $key['note_id']?>" class="waves-effect" id="getUser"><?php echo $key['topic']?></a>
<?php echo '</li>';?>
<li>Registration form
</li>
</ul>
</div>
</li>
<?php
}
?>
</ul>
If you want to fetch multiple records in a loop, you should use method ::fetch_row() (PHP.net).
So your code should look like this:
<ul class="collapsible collapsible-accordion">
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i>ENGLISH LANGUAGE<i class="fa fa-angle-down rotate-icon"></i></a>
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE subject = 'ENGLISH LANGUAGE' AND class = 'JHS 2'";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
while($row=$result->fetch_row()){
?>
<div class="collapsible-body">
<ul class="list-unstyled">
<?php echo '<li>'?><a data-id="<?php echo $key['note_id']?>" href="<?php echo $key['note_id']?>" class="waves-effect" id="getUser"><?php echo $key['topic']?></a>
<?php echo '</li>';?>
<li>Registration form
</li>
</ul>
</div>
</li>
<?php
}
?>
</ul>
By using ::fetch() method, you are fetching whole results array/object.

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.

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

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.

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>

Categories