PHP - make dynamic navbar - php

My Navbar (HTML only):
<ul class="navbar-nav m-auto">
<li class="nav-item active">Beranda</li>
<li class="nav-item">Paket Wisata</li>
<li class="nav-item">Foto</li>
<li class="nav-item">Kontak</li>
</ul>
My Navbar (php-version)
<ul class="navbar-nav m-auto">
<li <?php if ($active=='beranda') { echo 'class="nav-item active"'; } ?>>Beranda</li>
<li <?php if ($active=='karya') { echo 'class="nav-item active"'; } ?>>Paket Wisata</li>
<li <?php if ($active=='foto') { echo 'class="nav-item active"'; } ?>>Foto</li>
<li <?php if ($active=='kontak') { echo 'class="nav-item active"'; } ?>>Kontak</li>
</ul>
The PHP-version of the Navbar isn't working: please help me

you forgot to place else statement try this
<ul class="navbar-nav m-auto">
<li <?=($active=='beranda')?'class="nav-item active"':'nav-item';?>>Beranda</li>
<li <?=($active=='karya')?'class="nav-item active"':'nav-item';?>>Paket Wisata</li>
<li <?=($active=='foto')?'class="nav-item active"':'nav-item';?>>Foto</li>
<li <?=($active=='kontak')?'class="nav-item active"':'nav-item';?>>Kontak</li>
</ul>

Related

How to make Menu tab invisible if the user is not Admin

How to make some menu tab option is invisible if user is not Admin, eg: if ['user_level']>=5
<a class="navbar-brand" href="#">CBS</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active">DTC</li>
<li>View Proposal</li>
<li>Users details</li>
</ul>
<ul class="nav navbar-nav navbar-right">
I want to make only home.php menu tap visible to user only. Admin can see all the menu tab.
eg screen shot
Server side in php you can conditionally echo the related element.
Assuming you user auth is assigned to $userAuth var you can eg:
<a class="navbar-brand" href="#">CBS</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<?php if ( $userAuth == 'Admin' ) {
echo '<li class="active">DTC</li>';
}
?>
<li>View Proposal</li>
<li>Users details</li>
</ul>
<ul class="nav navbar-nav navbar-right">
You can do with 2 methods:
1) You have to give class hidden like this:
<style type="text/css">
.hidden {
display: none;
}
</style>
<?php
if ($user_level>=5) {
$hide = "hidden";
} else {
$hide = "";
}
?>
<a class="navbar-brand" href="#">CBS</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active <?=$hide?>">DTC</li>
<li class="<?=$hide?>">View Proposal</li>
<li class="<?=$hide?>">Users details</li>
</ul>
<ul class="nav navbar-nav navbar-right">
2) if else condition:
<a class="navbar-brand" href="#">CBS</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<?php if ($user_level>=5) { ?>
<li class="active">DTC</li>
<?php } else { ?>
<li class="active">DTC</li>
<li>View Proposal</li>
<li>Users details</li>
<?php } ?>
</ul>
<ul class="nav navbar-nav navbar-right">
Don't duplicate code - it is first rule. So better is to wrote:
(i persume that ['user_level'] is some value in array(); - here I call this array $user:
<a class="navbar-brand" href="#">CBS</a>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<!--first of all check if you get $user values-->
<?php if (isset($user)): ?>
<!-- display common elements for ALL USERS -->
<li class="active">DTC</li>
<!-- elements only for admins - users with level greater then 5-->
<?php if ($user['user_level'] >= 5): ?>
<li>View Proposal</li>
<li>Users details</li>
<?php endif; ?>
<?php endif; ?>
</ul>

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.

Php Sub Category loop html

I get the data from the database. but I can not write between li,ul html tags.There is a problem in the loop. How do I write between li ul categories and subcategories. I have this pdo mysql category function.
<?php
...
function kategoriVer($ustid=0){
$result = DB::get('SELECT * FROM urunler_kategori WHERE kategori_k='.$ustid.'');
echo '<ul class="drop-down">';
foreach($result as $kategori){
echo '<li class="drop">
'.$kategori->baslik_k.'';
kategoriVer($kategori->id_k);
}
echo '</li></ul>';
}
kategoriVer();
?>
output html:
<ul class="sub-menu">
<li class="menuparent">
Çadırlar
<ul class="sub-menu">
<li class="menuparent">Hi-Tech Çadırlar<ul class="sub-menu"></li></ul>
<li class="menuparent">Çelik Konstrüksiyon<ul class="sub-menu"></li></ul>
<li class="menuparent">Tribün Çadırlar<ul class="sub-menu"></li></ul>
<li class="menuparent">Yürüyüş Yolları<ul class="sub-menu"></li></ul>
</li>
</ul>
<li class="menuparent">Şemsiyeler<ul class="sub-menu"></li></ul>
<li class="menuparent">İklimlendirme<ul class="sub-menu"></li></ul>
</li>
</ul>
i want this output:
<ul class="sub-menu">
<li class="menuparent">
Headers
<ul class="sub-menu">
<li>Standard</li>
<li>No Topbar</li>
<li>Social Icons</li>
<li>Minimal</li>
<li>Classic</li>
</ul>
</li>
</ul>

PHP with or logical and adding a class to the li global navigation

I'm in the process on covering the navigation bar on all the pages to a global navigation with the PHP. I'm using PHP also to add a class and show the current page.
The challenge that I'm facing is the parent navigation selection. It's underlined when it's on the child page and the sub-nav is also selected. Such as I'm on the "History" page, child page to the "About Us" page. Both are underlined.
The challenge that I'm facing is when I move over to the next page, "Service Areas" or any of it's children, the "About Us" nav selection is still underline. I'm trying to use the || logic to prevent that from happening.
Below is the following code:
<li class="dropdown <?php if ($thisPage=='About Us'||'History'||'Mission Values'||'Process'||'Our People'||'Testimonials'||'Capstone Cares') echo 'active'; ?>">
Is there a way that I can deselect the parent nav when I'm under a different nav section?
Website is http://capstone.dgpehrson.com
Here is the rest of the code...
On individual pages I'm adding:
<?php $thisPage="About Us"; ?>
I'm changing the name according to the page.
Here is the Navigation code:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="dropdown <?php if ($thisPage=='About Us' || 'History' || 'Mission Values' || 'Process' || 'Our People' || 'Testimonials' || 'Capstone Cares') echo 'active'; ?>">
About Us<span class="caret"></span> <!-- Alink extentions that's been removed: data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" -->
<ul class="dropdown-menu">
<li <?php if ($thisPage=="History") echo "class=\"active\""; ?>>History</li>
<li <?php if ($thisPage=="Mission Values") echo "class=\"active\""; ?>>Mission & Values</li>
<li <?php if ($thisPage=="Process") echo "class=\"active\""; ?>>Process</li>
<li <?php if ($thisPage=="Our People") echo "class=\"active\""; ?>>Our People</li>
<li <?php if ($thisPage=="Testimonials") echo "class=\"active\""; ?>>Testimonials</li>
<li <?php if ($thisPage=="Capstone Cares") echo "class=\"active\""; ?>>Capstone Cares</li>
</ul>
</li>
<li class="dropdown <?php if ($thisPage=='Service Areas'||'Apartments'||'Capital'||'Development Services'||'Manufactured Housing'||'Complimentary') echo 'active'; ?>">
Service Areas<span class="caret"></span> <!-- Alink extentions that's been removed: data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" -->
<ul class="dropdown-menu">
<li <?php if ($thisPage=="Apartments") echo "class=\"active\""; ?>>Apartments</li>
<li <?php if ($thisPage=="Capital") echo "class=\"active\""; ?>>Capital</li>
<li <?php if ($thisPage=="Development Services") echo "class=\"active\""; ?>>Developement Services</li>
<li <?php if ($thisPage=="Manufactured Housing") echo "class=\"active\""; ?>>Manufactured Housing</li>
<li <?php if ($thisPage=="Complimentary") echo "class=\"active\""; ?>>Complimentary Value Analysis</li>
</ul>
</li>
<li class="dropdown">
Offerings<span class="caret"></span> <!-- Alink extentions that's been removed: data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" -->
<ul class="dropdown-menu">
<li>Apartment</li>
<li>Manufactured Housing</li>
<li>Multi-family Land</li>
</ul>
</li>
<li class="dropdown">
Market Reports<span class="caret"></span> <!-- Alink extentions that's been removed: data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" -->
<ul class="dropdown-menu">
<li>Florida</li>
<li>Kentucky</li>
<li>North Carolina</li>
<li>South Carolina</li>
<li>Tennessee</li>
<li>Virginia</li>
</ul>
</li>
<li>News</li>
<li>Careers</li>
<li>Contact Us</li>
</ul>
You have to have the compare between each 'or'. The string is just evaluating to a true.
if ($thisPage=='About Us'||$thisPage=='History'||$thisPage=='Mission Values'||$thisPage=='Process'||$thisPage=='Our People'||$thisPage=='Testimonials'||$thisPage=='Capstone Cares') echo 'active';

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