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

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>

Related

Navbar Breaks PHP redirect

I have a navbar from bootstrap3.3. I also have various forms on this page which redirects with header location when a form is submitted. Whenever I add any sort of additional form or dropdown to my navbar(as opposed to the page body), the header location stops redirecting (the query still runs in the db as normal).
They should be completely independant of each other so I'm confused if this is a bug with bs 3.3? Or is my code flawed/have I made a stupid mistake?
form query works perfectly, but header location only if my navbar is relatively simple..
$result = $this->db_query($query);
if($result){
header("Location: https://somepage.com?message=submitted");
}
still works with this:
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="https://somepage.com">Brand</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li >Home</li>
<li>About</li>
<li>Contact</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Order</li>
<li>Collections</li>
<li>Summary</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
but won't work with this (tried form search and dropdown separately and together)
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="https://somepage.com">Brand</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li >Home</li>
<li>About</li>
<li>Contact</li>
<li class="dropdown">
Filter<span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>test1</li>
<li>test2</li>
<li>test3</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><form class="form-inline" method="POST" action="">
<div class="form-group">
<input type="text" class="form-control" id="search" name="search" placeholder="Search">
</div>
<button type="submit" name="search" class="btn btn-secondary mb-2">Search</button>
</form></li>
<li>Order</li>
<li>Collections</li>
<li>Summary</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>

Drop down menu does not work on username

I have made a login system and I am trying to add a dropdown to the username for settings. However, I can make it look like it has a dropdown but no menus appear.
<?php else:?>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">A09hopper.xyz</a>
</div>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Page 1-1</li>
<li>Page 1-2</li>
<li>Page 1-3</li>
</ul>
</li>
<li>Page 2</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<?php if(!empty($_SESSION['username'])):?>
<li class="dropdown">
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="glyphicon glyphicon-user"> </span><?php echo $_SESSION['username'];?><span class="caret"></span></a></li>
<ul class="dropdown-menu">
<li>1-1</li>
<li>1-2</li>
</ul>
</li>
<?php else:?>
<li><span class="glyphicon glyphicon-user"></span> Sign Up</li>
<?php endif;?>
</ul>
</div>
</nav>
<?php endif; ?>
I have tried everything I can think of but to no avail.
I have just fixed my issue. The code below is the fix.
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">A09hopper.xyz</a>
</div>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Page 1-1</li>
<li>Page 1-2</li>
<li>Page 1-3</li>
</ul>
</li>
<li>Page 2</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<?php if(!empty($_SESSION['username'])):?>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="glyphicon glyphicon-user"> </span><?php echo $_SESSION['username'];?> <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Page 1-1</li>
<li>Page 1-2</li>
<li>Page 1-3</li>
</ul>
</li>
<?php else:?>
<li><span class="glyphicon glyphicon-user"></span> Sign Up</li>
<?php endif;?>
</ul>
</div>
</nav>

How to show different header menu in the different page using php?

I have two PHP scripts:
header.php
contact_us.php
In Index.php when the user clicks the menu, it smoothly slides down.
But for the "Contact Us" page I don't want that behavior; because it is so simple, containing only a link, I just want it to come up quickly and remain until the home link is clicked in the menu.
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<?php
if(basename($_SERVER['SCRIPT_FILENAME']) == 'index.php'){
?>
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>
<?php
}else{
?>
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>
<?php
}
?>
</div>
For else condition when you are on contact-us page.
Change URL as below:
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>

Keep navigation bar an all pages

hope you fine and well,
i have a navigation bar like this :
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>
how i can show this bar in all my html and php pages !
regards.
Put the render code in a my_navbar.php
echo '<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>';
and then in all php file you need the navbar put
<?php
include_once "my_navbar.php"; // this will include a.php
?>
Use include_once for avoiding multiple include
To expand on scaisEdge's comment, here's how you can do this in an include.
In nav.php (name it however you want):
<?php
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>
?>
Then where you want the navigation:
<?php include('nav.php'); ?>
You can use include(), i.e.:
navigation.php
<?php
echo <<< EOF
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>
EOF;
anotherfile.php
<?php
include("navigation.php");
//the rest of the code...
include vs include_once:
The include_once and require_once functions are slower than include
and require, simply because they keep track of the files that have
already been included, to avoid including them more than once.

I Want to Hide a Link in my Navbar While Logged In (Using CodeIgniter as my framework)

I'm attempting to hide the "Register" link in my navbar after the page checks if someone is logged in. I'm using CodeIgniter as my framework.
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Home <span class="sr-only">(current)</span></li>
<li>Reports <span class="sr-only">(current)</span></li>
<li >Register <span class="sr-only">(current)</span></li>
</ul>
I used this to check if someone is logged in before it shows the logout button and it works fine and dandy.
<?php if($this->session->userdata('logged_in')): ?>
<ul class="nav navbar-nav navbar-right">
<li>Logout</li>
</ul>
<?php endif;?>
However, I've seen to run into a snag when trying to do it to hide the Register link. Any help would be much appreciated!
Its should be like this
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Home <span class="sr-only">(current)</span></li>
<li>Reports <span class="sr-only">(current)</span></li>
<?php if(!$this->session->userdata('logged_in')){ ?>
<li >Register <span class="sr-only">(current)</span></li>
<?php } ?>
</ul>

Categories