How to make if condition in HTML sidebar in PHP - php

I have a sidebar as follows in HTML,
<div class="sidebar">
<h2 style="font-family: Verdana;">Dashboard</h2>
<ul>
<li><i class="fa fa-home"></i> Home</li>
<li><i class="fa fa-paper-plane"></i> dummy1</li>
<li><i class="fa fa-mobile"></i> dummy2</li>
<li><i class="fa fa-phone"></i> dummy3</li>
<li><i class="fa fa-plug"></i> dummy4</li>
</ul>
</div>
I need to check the username and show the tab.By using below code part I could get the current user logged in. I need to check whether below value is equal to John if and only if I need to show the tabs.
<?php echo htmlspecialchars($_SESSION["username"]); ?>
As an example,
if( htmlspecialchars($_SESSION["username"])=='John'){
<li><i class="fa fa-paper-plane"></i> dummy1</li>
<li><i class="fa fa-mobile"></i> dummy2</li>
<li><i class="fa fa-phone"></i> dummy3</li>
<li><i class="fa fa-plug"></i> dummy4</li>
}
Can someone show me how to achieve this with php and HTML?

You're nearly there - just wrap the PHP bits in PHP tags.
Also as an aside, you don't need htmlspecialchars() unless you're outputting the value into a HTML document. When you're just using it in an if, you're not outputting it.
<?php
if( $_SESSION["username"] =='John'){
?>
<li><i class="fa fa-paper-plane"></i> dummy1</li>
<li><i class="fa fa-mobile"></i> dummy2</li>
<li><i class="fa fa-phone"></i> dummy3</li>
<li><i class="fa fa-plug"></i> dummy4</li>
<?php
}
?>

Related

Write php file part name from jQuery $this value

I'm trying to 'include' a php file in a html template when a nav-item is selectd. The code below make active the item on click it. What I want to do is, at the same time it becomes active,load the php file with de same name than '$this' value is. I mean:
on click in tab 'two' ($this value), load in div '#contFl' the php file named 'two.php' at time its become 'active'; load 'one.php' on click in 'one' item;...
Tried a few ways of text estructure in 'load(...)' -last one on last code line- with no results. Pleasefull to have some help.
<script type="text/javascript">
$(document).ready(function(){
$("ul.navbar-nav > li").click(function(e){
$("ul.navbar-nav > li").removeClass("active");
$(this).addClass("active");
$("#contFl").load('"'+$(this).val()+'.php');
});
})
HTML code of navbar
<div id="collapsibleNavbar" class="collapse navbar-collapse justify-content-around">
<ul id="collUl" class="navbar-nav col-12 col-lg-auto me-lg-auto mb-1 mb-md-0">
<li class="nav-item nav-link active" id="uno" role="button"><i class="fa fa-home" aria-hidden="true"></i> Inicio</li>
<li class="nav-item nav-link" id="dos" role="button"><i class="fa fa-envelope" aria-hidden="true"></i> Dos</li>
<li class="nav-item dropdown" id="tres"><a class="nav-link dropdown-toggle px-2" href="#" role="button" data-bs-toggle="dropdown"><i class="fa fa-list" aria-hidden="true"></i> Tres</a>
<ul class="dropdown-menu">
<a class="dropdown-item" href="index.php"><i class="fa fa-home" aria-hidden="true"></i> Inicio</a>
<a class="dropdown-item" href="tres.php"><i class="fa fa-list" aria-hidden="true"></i> Tres</a>
<a class="dropdown-item" href="dos.php"><i class="fa fa-envelope" aria-hidden="true"></i> Dos</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="cuatro.php"><i class="fa fa-user" aria-hidden="true"></i> Cuatro</a>
</ul>
</li>
<li class="nav-item nav-link" id="cuatro" role="button"><i class="fa fa-user" aria-hidden="true"></i> Cuatro</li>
</ul>
Code in template where include new php file.
<?php
include(ASSETS."/navbar.php");
include(ASSETS."/banner.php")?>
<div id="contFl" class="container-fluid">
<!-- HERE THE NEW 'INCLUDE' -->
</div>

Why does my PHP tag does not work even though i (think i) did everything right?

I have a php file (converted from html) with a php tag inside that does not work, for some reason...
Here is part of my code, thanks a lot for your help...
<footer class="w3-container w3-blue w3-center w3-margin-top">
<p>Find me on social media.</p>
<a href="https://facebook.com/maximilien.ollier">
<i class="fa fa-facebook-official w3-hover-opacity"></i><a/>
<a href="https://www.instagram.com/mxollier/">
<i class="fa fa-instagram w3-hover-opacity"></i><a/>
<a href="https://twitter.com/Maxollier">
<i class="fa fa-twitter w3-hover-opacity"></i><a/>
<a href="https://www.linkedin.com/in/maximilien-ollier-526094154/">
<i class="fa fa-linkedin w3-hover-opacity"></i><a/>
<a href="https://www.reddit.com/user/Heroic_watermelon">
<i class="fa fa-reddit w3-hover-opacity"></i><a/>
<i class="fa fa-wechat w3-hover-opacity"></i><a/>
<a> Copyright ©
<?php echo date("Y")." "; ?>
<?php bloginfo('name')." "; ?>. All Rights Reserved.</a>
</footer>

repeat div and UL 3 times but li will repeat 7 times

I have a HTML structure with bootstrap 3 columns, which all div repeat 3 times only, but all div have one ul and each ul has 7 li.
I want to show the dynamic list according to the below HTML structure:
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> Mechanical Engineering Jobs</li>
<li><i class="fa fa-angle-right"></i> BPO Jobs</li>
<li><i class="fa fa-angle-right"></i> Networking Jobs</li>
<li><i class="fa fa-angle-right"></i> Java Jobs</li>
<li><i class="fa fa-angle-right"></i> Online Marketing Jobs</li>
<li><i class="fa fa-angle-right"></i> Animation Jobs</li>
<li><i class="fa fa-angle-right"></i> Design Engineer Jobs</li>
</ul>
</div>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> Analytics Jobs</li>
<li><i class="fa fa-angle-right"></i> UI/UX Jobs</li>
<li><i class="fa fa-angle-right"></i> NLP Jobs</li>
<li><i class="fa fa-angle-right"></i> Marketing Jobs</li>
<li><i class="fa fa-angle-right"></i> Banking Jobs</li>
<li><i class="fa fa-angle-right"></i> MBA Jobs</li>
<li><i class="fa fa-angle-right"></i> Teaching Jobs</li>
</ul>
</div>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> Accounting Jobs</li>
<li><i class="fa fa-angle-right"></i> Retail Jobs</li>
<li><i class="fa fa-angle-right"></i> Travel Jobs</li>
<li><i class="fa fa-angle-right"></i> Merchandiser Jobs</li>
<li><i class="fa fa-angle-right"></i> Architecture Jobs</li>
<li><i class="fa fa-angle-right"></i> Banking Insurance Jobs</li>
<li><i class="fa fa-angle-right"></i> Music Jobs</li>
</ul>
</div>
For this I have seen the below answers and tried with the below code.
I have tried with the below code but it's not working as is should.
<?php
$sqlEng = mysql_query('select * from jobs_category');
$count = 1;
while($resEng = mysql_fetch_array($sqlEng)){
if ($count%3 == 1)
{
?>
<div class="col-md-4 padding-left">
<?php } ?>
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> <?php echo $resEng['name'];?></li>
</ul>
<?php if ($count%3 == 0)
{
?>
</div>
<?php } $count++; } ?>
Assuming you have only 21 records in the table.
try this.
<?php
//$sqlEng = mysql_query('select * from jobs_category');
$servername = "localhost:3306";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, "stack1");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sqlEng = $conn->query('select * from jobs_category');
$count = 1;
while($resEng = $sqlEng->fetch_assoc()){
if ($count%7 == 1)
{
?>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<?php } ?>
<li><i class="fa fa-angle-right"></i> <?php echo $resEng['name'];?></li>
<?php if ($count%7 == 0)
{
?>
</ul>
</div>
<?php } $count++;
} ?>
tested in my local.
If I get your question correctly, you are trying to spread the list items <li> A Job </li> across three lists <div> <ul> List Items </ul> </div>
<?php
$sqlEng = mysql_query('select * from jobs_category');
$count = 1;
$group_one = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
$group_two = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
$group_three = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
while($resEng = mysql_fetch_array($sqlEng)){
if($count > 3) {
$count = 1;
}
switch ($count) {
case 1:
$group_one .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
break;
case 2:
$group_two .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
break;
case 3:
$group_three .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
}
$count++;
}
$group_one .= "
</ul>
</div>";
$group_two .= "
</ul>
</div>";
$group_three .= "
</ul>
</div>";
echo $group_one . $group_two . $group_three;
?>
You want to consider using MySQl improved mysqli https://www.w3schools.com/php/php_ref_mysqli.asp

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