Add HTML Class after 3 elements in PHP - 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>';
}

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

Images cannot be shown in php

I'm working on one webpage project and using php to show information of computers. I stored the urls of images in one relation and could successfully retrieve them out with php. But they cannot be shown in the webpage. Here's my code:
<?php
require_once('db_setup_hetty.php');
$sql = "USE hzhu24;";
if ($conn->query($sql) === TRUE){
//echo "using Database hzhu24"
}else{
echo "Error using database:" . $conn->error;
}
$C1 = $_POST["C1"];
$C2 = $_POST["C2"];
$query = "SELECT * FROM Items where Category1 Like '%$C1%' and Category2 Like '%$C2%';";
$result = $conn->query($query);
if($result->num_rows > 0){
?>
<?php
while($row = $result->fetch_assoc()){
$url = $row['Pic_url'];
?>
<div class="col-sm-4">
<div class="thumbnail">
<!--span class="e-label"><div>Sale</div></span-->
<span class="service-link text-center">
<img src= $url>
<div class="list-inline">
<i class="fa fa-eye"></i>
<i class="fa fa-link"></i>
</div>
</span>
<div class="caption">
<div class="category"> <?php echo $C2; ?>
<div class="pull-right">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
</div>
</div>
<h3><?php echo $row['Item_name']; ?></h3>
<h4><?php echo $row['Brand']; ?></h4>
<!--strong>$899.00</strong-->
<div>Details</div>
</div>
</div>
</div>
<?php
}
}
else {
echo "Item not found";
}
echo "</table>"
?>
And the webpage looks like:
If your php version >= 5.4, you can just use short echo tag like this:
<?php
require_once('db_setup_hetty.php');
$sql = "USE hzhu24;";
if ($conn->query($sql) === TRUE){
//echo "using Database hzhu24"
}else{
echo "Error using database:" . $conn->error;
}
$C1 = $_POST["C1"];
$C2 = $_POST["C2"];
$query = "SELECT * FROM Items where Category1 Like '%$C1%' and Category2 Like '%$C2%';";
$result = $conn->query($query);
if($result->num_rows > 0){
?>
<?php
while($row = $result->fetch_assoc()){
$url = $row['Pic_url'];
?>
<div class="col-sm-4">
<div class="thumbnail">
<!--span class="e-label"><div>Sale</div></span-->
<span class="service-link text-center">
<img src=<?=$url;?>>
<div class="list-inline">
<i class="fa fa-eye"></i>
<i class="fa fa-link"></i>
</div>
</span>
<div class="caption">
<div class="category">
<?=$C2;?>
<div class="pull-right">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
</div>
</div>
<h3><?=$row['Item_name'];?></h3>
<h4><?=$row['Brand'];?></h4>
<!--strong>$899.00</strong-->
<div>Details</div>
</div>
</div>
</div>
<?php
}
}
else {
echo "Item not found";
}
echo "</table>"

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

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>

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