When session starts header file changes after refreshing the page - php

New to Codeigniter.
This is my header file. After getting logged in, the header file needs to be changed but it changes after I refresh the page. Same is when I logout. Kindly help.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<ul class="nav navbar-nav">
<li><a href=<?php echo base_url( 'form/home')?>>HOME</a></li>
<li><a href=<?php echo base_url( 'form/about')?>>ABOUT</a> </li>
<li><a href=<?php echo base_url( 'form/contact')?>>CONTACT</a></li>
</ul>
<?php
if(isset($_SESSION["name"])){
?>
<ul class="nav navbar-nav navbar-right">
<li><a href=<?php echo base_url( 'form/welcome')?>><?php echo $_SESSION["name"];?></a></li>
<li></span></li>
</ul>
<?php
}else{
?>
<ul class="nav navbar-nav navbar-right">
<li></span></li>
<li></span> </li>
</ul>
<?php
}
?>
</div>
</nav>
</body>
</html>

Change the if condition inorder to create session with a name.
if($query -> num_rows() == 1)
{
$row = $query->row();
$data = array('name' => $row->name);
$this->session->set_userdata('loggin_session',$data); // given a name to session(i.e loggin_session).
return true;
}
Now in your View change like following:
<?php
$session_data = $this->session->userdata('loggin_session');
if(isset($session_data['name'])){
?>
<ul class="nav navbar-nav navbar-right">
<li><a href=<?php echo base_url('form/welcome')?>><?php echo $_SESSION["name"];?></a></li>
<li></span></li>
</ul>
<?php }else{ ?>
<ul class="nav navbar-nav navbar-right">
<li></span></li>
<li></span> </li>
</ul>
<?php } ?>

Related

PHP - make dynamic navbar

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>

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 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>

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.

My $_SESSION don't work on my page why?

i am using $session[login] but $_SESSION seem like don't not work on my home page of html/php webpage . It don have show up anything . why? could anyone help me
and tell me why? is it the session name wrong? or need to input php inside?
<html>
<title>Home</title>
<head>
<link href="SSCdesign.css" rel="stylesheet" type="text/css">
<script src="script.js"> </script>
</head>
<body>
<header id="SSClogo"><img src="SSC_logo_v2.png"></header>
<div id='userbar'>
<?php
$_SESSION['login'] = true;
while($row = mysql_fetch_assoc($result))
{
$_SESSION['student_id'] = $row['studnet_id'];
$_SESSION['student_name'] = $row['student_name'];
}
?>
</div>
<div id='wrapper' align="center">
<nav id='cssmenu'>
<ul>
<li class='active'><a href='SSC.html'><span>Home</span></a></li>
<li><a href='advisors.html'><span>Advisors</span></a></li>
<li class='has-sub'><a href='pals.html'><span>PALS</span></a>
<ul>
<li><a href='registersession.html'><span>Register Session</span></a></li>
<li><a href='timetable.html'><span>Timetable</span></a></li>
<li><a href='feedback.html'><span>Feedback</span></a></li>
<li class='last'><a href='buzz.html'><span>Buzz</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='econsultation.html'><span>e-Consultation</span></a>
<ul>
<li><a href='appointment.html'><span>e-Appointment</span></a></li>
<li><a href='upcoming.html'><span>Upcoming List Appointment</span></a></li>
<li class='last'><a href='history.html'><span>History</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='workshop.html'><span>Workshop</span></a>
<ul>
<li><a href='calendar.html'><span>Calendar</span></a></li>
<li class='last'><a href='eportfolio.html'><span>e-Portfolio</span></a></li>
</ul>
</li>
<li><a href='facilities.html'><span>Facilities</span></a></li>
<li class='last'><a href='contactus.html'><span>Contact Us</span></a></li>
<li class='la'><a href='login.html'><span>Login</span></a></li>
</ul>
<div id="announcement">
<h2> <center> Announcement will be posted here </center> </h2>
<p> <center> This site is currently under development, do check it out soon! </center> </p>
</div>
<div id="slideshow">
<h2> <center> Slideshow will be here </center> </h2>
</div>
</nav>
<div class="push"></div>
</div>
</body>
</html>
You have to start your session before you use it with:
session_start();
So your code should looks something like this:
<?php
session_start();
$_SESSION['login'] = true;
(Also if you say you use: $session[login], it's wrong you would have to use it with: $session['login'] otherwise login is a constant which it isn't)
For more information see: http://php.net/manual/en/function.session-start.php

Categories