How to show menu according to session in php? - php

I have 2 tables admin and superadmin. I have same login page for both
users. i can login according to query.i had create sessions for both
users. each table have column name roleID. for superadmin roleID is
1, and for admin roleID is 2. Below is my code for login where i
create session. i used print_r and my session are working. Below is
my code for login page.
if (isset($_REQUEST['submit']))
{
$username = $_REQUEST['user'];
$password = $_REQUEST['pass'];
$sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '".$username."' AND `acc_pass` = '".$password."'");
$data = mysqli_fetch_array($sql);
$_SESSION['role0']=$data['roleId'];
$_SESSION['username']=$data['acc_name'];
$sql1 = mysqli_query($conn,"SELECT * FROM `superadmin` where `username` = '".$username."' AND `password` = '".$password."'");
$data1 = mysqli_fetch_array($sql1);
$_SESSION['role1']=$data1['roleId'];
if ($data>0)
{
header('Location: societyList.php');
}
elseif ($data1>0) {
header('Location: home.php');
}
else
{
header('Location: index.php');
echo 'incorrect login';
}
}
Now on home.php i have some menus to show accoording to roleID my
code for menu.
<div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
<div class="menu_section">
<h3>General</h3>
<ul class="nav side-menu">
<li><a><i class="fa fa-home"></i> Home <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Create Society</li>
</ul>
</li>
<li><a><i class="fa fa-home"></i> Master <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Units</li>
<li>Members</li>
<li>Parking Lots</li>
<li>Charges</li>
<li>Chart of Account</li>
<li>Interest Penalties</li>
<li>Billing Templates</li>
<li>Tax Structure</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Transactions <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Bill</li>
<li>Collection</li>
<li>Expenses</li>
<li>Journal</li>
<li>Bank Reco</li>
<li>Drop Box</li>
<li>Online Payment</li>
</ul>
</li>
<li><a><i class="fa fa-desktop"></i> Reports <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Income & Expenses</li>
<li>Balance Sheet</li>
<li>Cash Flow</li>
<li>Interest Calculation</li>
</ul>
</li>
<li><i class="fa fa-table"></i> Notices <span class="fa fa-chevron-down"></span>
</li>
<li><a><i class="fa fa-bar-chart-o"></i> Registers <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Form-I</li>
</ul>
</li>
<li><a><i class="fa fa-clone"></i> Forum <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Cultural Activity</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Domestic Help <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Request for Plumber</li>
<li>Request for Maid</li>
<li>Request for House Cleaner</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Emergency <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Ambulance</li>
<li>Fire Brigade</li>
<li>Police</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Helpdesk <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Request NOC for Tenancy</li>
<li>Request NOC for Home Loan</li>
<li>Request NOC for Mortgage as collateral</li>
<li>Request for Vehicle Parking</li>
<li>Application for Transfer</li>
<li>Request for waiver of interest</li>
<li>Request for waiver of a charge</li>
<li>Suggestion</li>
<li><a><i class="fa fa-edit"></i><span class="fa fa-chevron-down"></span>Complaint</a>
<ul class="nav child_menu">
<li>About leakage</li>
<li>About tenants issues</li>
<li>About parking nuisance
</ul>
</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> CFO Desk Assists <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Tenant Rating</li>
<li>Owners Rating</li>
<li>Your Reviews</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Masters <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Auto Bank reconcilliation</li>
<li>AMC Masters</li>
<li>Auto Adjust pending reference</li>
<li>Default GL for defined transactions</li>
</ul>
</li>
</ul>
</div>
</div>
As you can see lists. i want if $_SESSION['role0']=$data['roleId']; in session so first 4 lists will be visible for him only.
If $_SESSION['role1']=$data['roleId']; in session then rest of all lists will be display to him.
How to done it please help me with the same. I used if condition like
if{$_REQUEST($_SESSION['role0'])
echo 'some lists';
}
if i used like this if condition nothing will display.

The session data is note stored in the $_REQUEST.
$_SESSION['role0']=$data1['roleId'];
$_SESSION['role1']=$data1['roleId'];
Both 'role0' and 'role1' will have the same value. It can be simplified to:
$_SESSION['role']=$data1['roleId'];
Then use:
if( $_SESSION['role'] === 1) {
echo 'admin role 1';
echo 'Show first half of menu';
} elseif ($_SESSION['role'] === 2) {
echo 'Show second half of menu';
} else {
echo 'Other or missing admin value. show no menu';
}
SQL injection is possible the way it is currently written. Prepared statements would be a good thing to add next.

Related

Make a global sidebar?

I'm new at web programming was was wondering if you guys could help me. What I want I guess must be easy, but can't really find /how/.
I have my cute little sidebar that isn't even finished and is constantly needing to be changed. I already gave up on updating it until everything is ready:
<!-- sidebar menu -->
<div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
<div class="menu_section">
<h3>General</h3>
<ul class="nav side-menu">
<li><a><i class="fa fa-home"></i> Home <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Search</li>
<li>Dashboard</li>
<li>Lookup</li>
</ul>
</li>
<li><a><i class="fa fa-cubes"></i> Inventory <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Asset List
<li><a>Asset Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="create.php">Create Assets
</li>
<li>Update Assets
</li>
</ul>
</li>
</ul>
</li>
<li><a><i class="fa fa-sitemap"></i> Tickets <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Ticket Overview
<li><a>Ticket Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="sub_menu">Create a Ticket
</li>
<li>Update a Ticket
</li>
</ul>
</li>
<li><a>User Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="sub_menu">User List
</li>
<li>Create a User
</li>
<li>Update a User
</li>
</ul>
</li>
</ul>
<li><a><i class="fa fa-laptop"></i> Loans <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a>Loan Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="create.php">Create a Loan
</li>
<li>View Loans
</li>
</ul>
</li>
</ul>
</li>
</li>
</ul>
</div>
</div>
<!-- /sidebar menu -->
But the problem is that I'm doing it like an uncultured swine and keeping that in every single page where it is needed. And if I want to make a change, I need to edit every single page.
I was wondering if there was a way to have a class, or something, with my header, sidebar, and footer, and load them across all pages that needed them?
Thank you for your help!
You can extract the sidebar code into its own PHP file (sidebar.php) then include it where you need it with include or include_once
<html>
...
<body>
<?php include './sidebar.php' ?>
...
</body>
</html>
Using include is essentially the same as copy pasting the code into your file. I.e. It will be added where you include the code and it will be evaluated there as well
Its very simple, Save you sidebar in a php file lets call it sidebar.php, and add below code in your other PHP file where you want to show the sidebar.
<?php include_once('sidebar.php');?>
That's it.

Menu can't go directly to a page

I have menu which is has sub menus.
<li>
<i class="fa fa-flag-checkered" aria-hidden="true"></i> <span>Lead</span> <i class="icon-submenu lnr lnr-chevron-left"></i>
<div id="lead" class="collapse ">
<ul class="nav">
<li><i class="fa fa-quote-right" aria-hidden="true"></i><span> Quotation </span><i class="icon-submenu lnr lnr-chevron-left"></i>
<div id="quotation" class="collapse ">
<ul class="nav">
<li><i class="fa fa-dot-circle-o" aria-hidden="true"></i> Create Quotation</li>
<li><i class="fa fa-dot-circle-o" aria-hidden="true"></i> Quotation Summary</li>
<li><i class="fa fa-dot-circle-o" aria-hidden="true"></i> Send Quotation</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
When I clicked it, I can not go directly to the page. I have to right-clicked on it to go to the desired page.
Here's an example of my controller
public function cquotation(){
$data = $this->salesModel->showCustomer();
$this->load->view('v_navbar');
$this->load->view('v_leftside');
$this->load->view('v_cquotation',array('data'=>$data));
}
Do you guys have any idea what's wrong with it?
I'm totally confused
Why are you using data-toggle="collapse" class="collapsed" on every hyperlink?. your code has data-toggle="collapse" class="collapsed" in every hyperlink.Bootstrap will prevent the default click event of a href .So remove every data-toggle="collapse" class="collapsed" part of a href in your code and it will work.
<li>
<i class="fa fa-flag-checkered" aria-hidden="true"></i> <span>Lead</span> <i class="icon-submenu lnr lnr-chevron-left"></i>
<div id="lead" class="collapse ">
<ul class="nav">
<li><i class="fa fa-quote-right" aria-hidden="true"></i><span> Quotation </span><i class="icon-submenu lnr lnr-chevron-left"></i>
<div id="quotation" class="collapse ">
<ul class="nav">
<li></i> Create Quotation</li>
<li></i> Quotation Summary</li>
<li></i> Send Quotation</li>
</ul>
</div>
</li>
</ul>
</div>
</li>

How to set menu items as active in Yii framework?

I'm new to Yii framework..
Now i working with yii1.1.6 version.In admin panel menus i want to display as active menu while which menu item is selected.Please help me how to make menu item as active...
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li class="">
<i class="fa fa-fw fa-dashboard"></i> Home
</li>
<li class="">
<i class="fa fa-fw fa-bar-chart-o"></i> About Us
</li>
<li class="">
<i class="fa fa-fw fa-table"></i> Contact Us
</li>
</ul>
</div>
You can compare your controller id and action id like this:
if(Yii::app()->controller->id == 'site' && Yii::app()->controller->action->id == 'index'){
echo '<li class="active">';
}

Trouble Converting my current Menu into Bootstrapped Menu

Well, currently I've this main_menu table in my database.
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Oct 29, 2016 at 04:49 PM
-- Server version: 10.1.13-MariaDB
-- PHP Version: 5.6.20
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `database`
--
-- --------------------------------------------------------
--
-- Table structure for table `main_menu`
--
CREATE TABLE `main_menu` (
`id` bigint(20) NOT NULL,
`title` text COLLATE utf8_unicode_ci NOT NULL,
`link` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`parentid` bigint(20) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `main_menu`
--
ALTER TABLE `main_menu`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `main_menu`
--
ALTER TABLE `main_menu`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=53;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
And I have this code to make dropdown menu (not bootstrapped)
<?php
error_reporting(0);
ini_set('display_errors', 0);
header('Content-Type: text/html; charset=utf-8');
//Set the database connection
// $con1 = mysqli_connect('localhost','root','');
$con1 = mysqli_connect('localhost', '', '');
// mysqli_select_db($con1, 'database_name') or die(mysqli_error($con1));
mysqli_select_db($con1, 'database_name') or die(mysqli_error($con1));
//select all rows from the main_menu table
mysqli_query($con1, "SET NAMES utf8");
$result = mysqli_query($con1, "select id,title,parentid,link from main_menu order by id asc");
//create a multidimensional array to hold a list of menu and parent menu
$menu = array(
'menus' => array(),
'parent_menus' => array()
);
//build the array lists with data from the menu table
while ($row = mysqli_fetch_assoc($result)) {
//creates entry into menus array with current menu id ie. $menus['menus'][1]
$menu['menus'][$row['id']] = $row;
//creates entry into parent_menus array. parent_menus array contains a list of all menus with children
$menu['parent_menus'][$row['parentid']][] = $row['id'];
}
mysqli_set_charset($con1,"utf8");
// Create the main function to build milti-level menu. It is a recursive function.
function buildMenu($parent, $menu) {
$html = "";
if (isset($menu['parent_menus'][$parent])) {
$html .= "<ul>";
foreach ($menu['parent_menus'][$parent] as $menu_id) {
if (!isset($menu['parent_menus'][$menu_id])) {
$html .= "<li><a href='" . $menu['menus'][$menu_id]['link'] . "'>" . $menu['menus'][$menu_id]['title'] . "</a></li>";
}
if (isset($menu['parent_menus'][$menu_id])) {
$html .= "<li><a href='" . $menu['menus'][$menu_id]['link'] . "'>" . $menu['menus'][$menu_id]['title'] . "</a>";
$html .= buildMenu($menu_id, $menu);
$html .= "</li>";
}
}
$html .= "</ul>";
}
return $html;
}
?>
It's working until now, but the problem is I've tried copy/paste the code into the bootstrap theme called Karma that I purchased recently, and I can't figure out how to make the menu based on my main_menu table.
Here's the sample bootstrapped navigation code
<div class="sidebar-module">
<nav class="sidebar-nav-v2">
<ul>
<li class="page-arrow active-page">
<i class="fa fa-dashboard"></i> Dashboard <span class="indicator-pill">32</span>
</li>
<li>
<i class="fa fa-sun-o"></i> Statistics<span class="indicator-dot">2</span>
</li>
<li>
<i class="fa fa-bar-chart-o"></i> Charts
</li>
<li>
<i class="fa fa-calendar-o"></i> Calendar
</li>
<li>
<i class="fa fa-envelope-o"></i> Form Elements <i class="fa fa-caret-left pull-right"></i>
<!-- * sub menu * -->
<ul>
<li>
Forms
</li>
<li>
Forgot Form
</li>
<li>
Login Form
</li>
<li>
Login 2 Form
</li>
<li>
Reset Form
</li>
<li>
Signup Form
</li>
<li>
Wizard
</li>
<li>
WYSIWYG
</li>
</ul>
</li>
<li>
<i class="fa fa-file-o"></i> Pages <i class="fa fa-caret-left pull-right"></i>
<!-- * sub menu * -->
<ul>
<li>
Billing
</li>
<li>
Comments
</li>
<li>
Clients
</li>
<li>
FAQs
</li>
<li>
Files
</li>
<li>
Planning
</li>
<li>
Social
</li>
<li>
Ticket Support
</li>
<li>
Timeline
</li>
<li>
Wiki
</li>
</ul>
</li>
<li>
<i class="fa fa-star-o"></i> UI Elements<i class="fa fa-caret-left pull-right"></i>
<!-- * sub menu * -->
<ul>
<li>
Buttons & Icons
</li>
<li>
Notifications
</li>
<li>
Dropdown & Menu
</li>
<li>
Misc
</li>
<li>
Tabs
</li>
<li>
Toolbars
</li>
</ul>
</li>
<li class="seperator">
<!-- * seperator line * -->
</li>
<li>
<i class="fa fa-picture-o"></i> Media
</li>
<li>
<i class="fa fa-wrench"></i> Modules
</li>
<li>
<i class="fa fa-table"></i> Tables
</li>
<li>
<i class="fa fa-th"></i> Page Layout
</li>
<li>
<i class="fa fa-warning"></i> Error Pages <i class="fa fa-caret-left pull-right"></i>
<!-- * sub menu * -->
<ul>
<li>
400
</li>
<li>
401
</li>
<li>
403
</li>
<li>
404
</li>
<li>
500
</li>
<li>
503
</li>
</ul>
</li>
<li class="menu-label">
<div class="spacer-20"></div>
Some group label
<div class="spacer-10"></div>
</li>
<li>
<i class="fa fa-map-marker"></i> Maps
</li>
<li>
<i class="fa fa-columns"></i> Widgets
</li>
<li>
<i class="fa fa-sitemap"></i> Sitemap
</li>
</ul>
</nav>
<!-- End .sidebar-nav-v1 -->
</div>
<!-- End .sidebar-module -->
Can anyone help me converting my old menu into bootstrapped navigation?
There is no bootstrap dropdown class in your html code. here is snippet. make sure to include jquery, bootstrap in your code(which is included in this snippet) the link tag comes under head tag and script tag under body tag.
.navbar-nav > li:hover ul.dropdown-menu, .menu li:hover ul.dropdown-menu{
display: block;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Start Bootstrap</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="page-arrow active-page">
<i class="fa fa-dashboard"></i> Dashboard <span class="indicator-pill">32</span>
</li>
<li>
<i class="fa fa-sun-o"></i> Statistics<span class="indicator-dot">2</span>
</li>
<li>
<i class="fa fa-bar-chart-o"></i> Charts
</li>
<li>
<i class="fa fa-calendar-o"></i> Calendar
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><i class="fa fa-envelope-o"></i> Form Elements <i class="fa fa-caret-left pull-right"></i> <span class="caret"></span></a>
<!-- * sub menu * -->
<ul class="dropdown-menu">
<li>
Forms
</li>
<li>
Forgot Form
</li>
<li>
Login Form
</li>
<li>
Login 2 Form
</li>
<li>
Reset Form
</li>
<li>
Signup Form
</li>
<li>
Wizard
</li>
<li>
WYSIWYG
</li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><i class="fa fa-file-o"></i> Pages <i class="fa fa-caret-left pull-right"></i> <span class="caret"></span></a>
<!-- * sub menu * -->
<ul class="dropdown-menu">
<li>
Billing
</li>
<li>
Comments
</li>
<li>
Clients
</li>
<li>
FAQs
</li>
<li>
Files
</li>
<li>
Planning
</li>
<li>
Social
</li>
<li>
Ticket Support
</li>
<li>
Timeline
</li>
<li>
Wiki
</li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><i class="fa fa-star-o"></i> UI Elements<i class="fa fa-caret-left pull-right"></i> <span class="caret"></span></a>
<!-- * sub menu * -->
<ul class="dropdown-menu">
<li>
Buttons & Icons
</li>
<li>
Notifications
</li>
<li>
Dropdown & Menu
</li>
<li>
Misc
</li>
<li>
Tabs
</li>
<li>
Toolbars
</li>
</ul>
</li>
<li class="seperator">
<!-- * seperator line * -->
</li>
<li>
<i class="fa fa-picture-o"></i> Media
</li>
<li>
<i class="fa fa-wrench"></i> Modules
</li>
<li>
<i class="fa fa-table"></i> Tables
</li>
<li>
<i class="fa fa-th"></i> Page Layout
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><i class="fa fa-warning"></i> Error Pages <i class="fa fa-caret-left pull-right"></i> <span class="caret"></span></a>
<!-- * sub menu * -->
<ul class="dropdown-menu">
<li>
400
</li>
<li>
401
</li>
<li>
403
</li>
<li>
404
</li>
<li>
500
</li>
<li>
503
</li>
</ul>
</li>
<li class="menu-label">
<div class="spacer-20"></div>
Some group label
<div class="spacer-10"></div>
</li>
<li>
<i class="fa fa-map-marker"></i> Maps
</li>
<li>
<i class="fa fa-columns"></i> Widgets
</li>
<li>
<i class="fa fa-sitemap"></i> Sitemap
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
basically your html code should look like this image

How can I edit navigation bar to take out a link when admin user signs in?

The nav prior to sign in or register has the following links:
Home,
Blog,
Write a Blog,
Sign in,
Register,
When a normal user signs in the nav links will change to:
Hello (name of user),
Home,
Blog,
Write a blog,
Logout,
When the admin logs in the nav displays the following:
Hello (name of user),
Home,
Blog,
Write a blog,
Admin,
Logout,
My issue is I would like to remove the link 'write a blog' when the admin signs in. Any advice on how I can adapt my code to do this would be great. Below is the HTML & Php code in the header.php:
<header>
<div class="wrap-header zerogrid">
<div id="logo"><img src="/assets/images/logo_blog.png"/></div>
<nav>
<div class="wrap-nav">
<div class="menu">
<ul class="list-unstyled">
<? if ($_SESSION['user']['first_name']): ?>
<li><i class="icon fa fa-user"></i> Hello <?= $_SESSION['user']['first_name'] ?></li>
<? endif ?>
<li><i class="icon fa fa-home"></i> Home</li>
<li><i class="icon fa fa-book"></i> Blog</li>
<li><i class="icon fa fa-pencil"></i> Write a Blog</li>
<? if ($_SESSION['user']['level'] >= 2): ?>
<li><i class="icon fa fa-pencil"></i> Admin</li>
<? endif ?>
<? if ($_SESSION['user']['first_name']): ?>
<li><i class="icon fa fa-sign-in"></i> Logout</li>
<? else: ?>
<li><i class="icon fa fa-sign-in"></i> Login</li>
<li><i class="icon fa fa-pencil"></i> Register</li>
<? endif ?>
</ul>
</div>
</div>
</nav>
</div>
</header>
It is probably something easy, I do tend to over complicate things! Thank you in advance.
Here's one approach where variables are set and validated before processing markup content. May provide a clear, clean code.
<?php
// 1. get session user object
$user = (isset($_SESSION['user'])) ? $_SESSION['user'] : null;
// 2. set vars
if($user) {
if(isset($user['first_name'])) {
$first_name = $user['first_name'];
}
if(isset($user['level'])) {
$level = $user['level'];
}
}
// 3. set flag (optional) or access $level directly
if(isset($level)) {
$isAdmin = ($level === "Admin") ? true: false;
}
// normal user: Hello (name of user), Home, Blog, Write a blog, Logout,
// admin logs : Hello (name of user), Home, Blog, Write a blog, Admin, Logout,
?>
and the markups...
<header>
<div class="wrap-header zerogrid">
<div id="logo"><img src="/assets/images/logo_blog.png"/></div>
<nav>
<div class="wrap-nav">
<div class="menu">
<ul class="list-unstyled">
<!-- code omited -->
<?php
if(isset($isAdmin) and $isAdmin) {
// display markup for Admin <li>
}
?>
</ul>
</div>
</div>
</nav>
</div>
</header>
Hope this helps.
Thank you for your replies. It really was something simple solved with an if statement:
<nav>
<div class="wrap-nav">
<div class="menu">
<ul class="list-unstyled">
<? if ($_SESSION['user']['first_name']): ?>
<li><i class="icon fa fa-user"></i> Hello <?= $_SESSION['user']['first_name'] ?></li>
<? endif ?>
<li><i class="icon fa fa-home"></i> Home</li>
<li><i class="icon fa fa-book"></i> Blog</li>
<? if ($_SESSION['user']['level'] < 2): ?>
<li><i class="icon fa fa-pencil"></i> Write a Blog</li>
<? else: ?>
<li><i class="icon fa fa-pencil"></i> Admin</li>
<? endif ?>
<? if ($_SESSION['user']['first_name']): ?>
<li><i class="icon fa fa-sign-in"></i> Logout</li>
<? else: ?>
<li><i class="icon fa fa-sign-in"></i> Login</li>
<li><i class="icon fa fa-pencil"></i> Register</li>
<? endif ?>
</ul>
</div>
</div>
</nav>

Categories