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

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

Related

How to make if condition in HTML sidebar in 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
}
?>

How to show menu according to session in 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.

Show active navigation for current page

i'm having some problems with my html, php code. With this code, my nav will show the active li but the other pages which are not active won't have style or links.
<!--/. PHP "active" -->
<?php
$current_url = basename($_SERVER['PHP_SELF']);
$active = "class=\"active-menu\"";
?>
<!--/. PHP "active" -->
<nav class="navbar-default navbar-side" role="navigation">
<div class="sidebar-collapse">
<ul class="nav" id="main-menu">
<li>
<?php if ($current_url == "home.php") { ?>
<a <?php echo $active;?> href="home.php"><?php } ?><i class="fa fa-dashboard"></i> Dashboard</a>
</li>
<li>
<?php if ($current_url == "new.php") { ?>
<a <?php echo $active;?> href="new.php"><?php } ?><i class="fa fa-qrcode"></i> Nieuw item toevoegen</a>
</li>
<li>
<?php if ($current_url == "show.php") { ?>
<a <?php echo $active;?> href="show.php"><?php } ?><i class="fa fa-qrcode"></i> Bekijk inventaris</a>
</li>
<li>
<?php if ($current_url == "profile.php") { ?>
<a <?php echo $active;?> href="profile.php"><?php } ?><i class="fa fa-user"></i> Gebruikersprofiel</a>
</li>
<li>
<i class="fa fa-sitemap"></i> Gebruikers<span class="fa arrow"></span>
<ul class="nav nav-second-level">
<li>
Beheerders
</li>
<li>
ICT - Verantwoordelijken
</li>
<li>
Raadplegers
</li>
</ul>
</li>
<li>
<?php if ($current_url == "empty.php") { ?>
<a <?php echo $active;?> href="empty.php"><?php } ?><i class="fa fa-fw fa-file"></i> Lege pagina</a>
</li>
</ul>
</div>
</nav>
the pages with no styling will also not have a link. The active-menu class has the styling.
Your if condition it is wrong.
Try something like this in every <li>.
<li>
<a <?php echo $current_url == "home.php" ? $active : ''; ?> href="home.php"><i class="fa fa-dashboard"></i> Dashboard</a>
</li>
<li>
<a href="new.php" <?php if ($current_url == "new.php") {echo $active;}?>>
<i class="fa fa-qrcode"></i> Nieuw item toevoegen
</a>
</li>
Put your list like this
You should make another class, like $not_active = "class=\"not-active-menu\"";
And add that to your code
<li>
<a href="something.php" <?php if ($current_url == "something.php") {echo $active;} else { echo $not_active; }?>>
<i class="fa fa-qrcode"></i> something
</a>
</li>

Add HTML Class after 3 elements in PHP

My HTML should seen like this;
<div class="row">
<div class="col-xs-6">
<ul class="list-unstyled">
<li><i class="fa fa-check-circle"></i> Car</li>
<li><i class="fa fa-check-circle"></i> Google</li>
<li><i class="fa fa-check-circle"></i> Building</li>
</ul>
</div>
<div class="col-xs-6">
<ul class="list-unstyled">
<li><i class="fa fa-check-circle"></i> Bathroom</li>
<li><i class="fa fa-check-circle"></i> Facebook</li>
<li><i class="fa fa-check-circle"></i> Twitter</li>
</ul>
</div>
</div>
In database, all data is in same field with there is a comma between them(e.g. Car,Google,Building,Bathroom, Facebook, Twitter).
So, I'm using this code to seperate them;
<?php echo str_replace(',', '</li><li><i class="fa fa-check-circle"></i>', $data); ?>
I need to get the same result with HTML, how can I do this?
Thanks in advance.
$arrData = explode(",", $data); // comma separated list of words
$i = 0; // temp counter
echo '<div class="row">';
foreach ($arrData as $word)
{
if ($i==0) { echo '<div class="col-xs-6"><ul class="list-unstyled">'; } // open first html containers
echo '<li><i class="fa fa-check-circle"></i>'.$word.'</li>'; // create li
$i++; // increase counter
if ($i==2) // this is the 3rd element in loop
{
echo '</ul></div>'; // close html containers
$i=0; // reset counter, so proccess repeats
}
}
echo '</div>';
This code will do what you need, as long as you always have 6 words in your array (or 9, 12, 15, etc).
<?php
$v1=explode(",",$data);
$data0=$v1[0];
$data1=$v1[1];
$data2=$v1[2];
$data3=$v1[3];
$data4=$v1[4];
$data5=$v1[5];
?>
<div class="row">
<div class="col-xs-6">
<ul class="list-unstyled">
<li><i class="fa fa-check-circle"></i> <?php echo $data0; ?></li>
<li><i class="fa fa-check-circle"></i> <?php echo $data1; ?></li>
<li><i class="fa fa-check-circle"></i> <?php echo $data2; ?></li>
</ul>
</div>
<div class="col-xs-6">
<ul class="list-unstyled">
<li><i class="fa fa-check-circle"></i> <?php echo $data3; ?></li>
<li><i class="fa fa-check-circle"></i> <?php echo $data4; ?></li>
<li><i class="fa fa-check-circle"></i> <?php echo $data5; ?></li>
</ul>
</div>
</div>
You need to explode and loop through it... look at this and it should help you understand
$pieces = explode(",", $data);
foreach($pieces as $value){
echo '<li><i class="fa fa-check-circle"></i>'.$value.'</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