I have an unordered list which values are generated from a database using php and mysql.
<div id="articles">
<ul>
<?php foreach ($articles as article) { ?>
<li> <?php echo $article['name']; ?> </li>
<?php } ?>
</ul>
</div>
I'd like to use jQuery to add a css class to every item in that list except the last one
//then you can select all `li` elements under `#articles` except the last one like
jQuery(function($) {
$('#articles li:not(:last)').addClass('myclass')
})
.myclass {
background-color: lightgrey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="articles">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
You can do it with css only
#articles li {
background-color: lightgrey;
}
#articles li:last-child {
background-color: white;
}
<div id="articles">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
<div id="articles">
<ul>
<?php
$count =count($articles);
foreach ($articles as article) {
$counter++;
If($count==$counter)
$class='something';
Else
$class='';
?>
<li> <?php echo $article['name']; ?> </li>
<?php
}
?>
</ul>
</div>
Use class variable in li now.
You can achieve same thing using PHP also by adding one more condition in your code.
foreach ($articles as $article) {
if(end($articles) == $article) {
?>
<li> <?php echo $article['name']; ?> </li>
<?php
}
else{ ?>
<li class="myclass"> <?php echo $article['name']; ?> </li>
<?php
}
}
Related
I'm trying to style the active link with is_page() function. And it works for the first nav item. But when I click the "om" link it will get styling, but the styling for "hem" remains. But when I click the "blogg" and "tutorial" links none of the links gets any styling.
This is the code that I have in header.php.
<nav>
<button class="menuButton">Meny</button>
<div class="menu">
x
<ul>
<h2>Meny</h2>
<li class="<?php if (is_page("")) { echo "active-page"; } ?>">Hem</li>
<li class="<?php if (is_page("/om")) { echo "active-page"; } ?>">Om</li>
<li class="<?php if (is_page("/blogg")) { echo "active-page"; } ?>">Blogg arkiv</li>
<li class="<?php if (is_page("/tutorial")) { echo "active-page"; } ?>">Tutorials arkiv</li>
</ul>
</div>
</nav>
Is this the wrong way to use the is_page()function? I'm a little lost here. The styling that gets applied is just a simple text-decoration: underline; CSS style.
Try this
<nav>
<button class="menuButton">Meny</button>
<div class="menu">
x
<ul>
<h2>Meny</h2>
<li class="<?php if (is_page("")) { echo "active-page"; } ?>">Hem</li>
<li class="<?php if (is_page("om")) { echo "active-page"; } ?>">Om</li>
<li class="<?php if (is_page("blogg")) { echo "active-page"; } ?>">Blogg arkiv</li>
<li class="<?php if (is_page("tutorial")) { echo "active-page"; } ?>">Tutorials arkiv</li>
</ul>
</div>
</nav>
I'm trying to make an active navigation bar using PHP where the current page will be highlighted or colored in the navigation bar. The code I used:
<ul class="topmenu">
<li <?php if($_GET['page']="home") { ?> class="active" <?php } ?>>
<b>Bienvenue</b>
</li>
<li <?php if($_GET['page']="livres") { ?> class="active" <?php } ?>>
<b>Livres</b>
</li>
<li <?php if($_GET['page']="bibliotheque") { ?> class="active" <?php } ?>>
<b>Bibliothèque</b>
</li>
<li <?php if($_GET['page']="universite") { ?> class="active" <?php } ?>>
<b>L'université</b>
</li>
<li <?php if($_GET['page']="contact") { ?> class="active" <?php } ?>>
<b>Contact</b>
</li>
</ul>
The code will put the attribut in the class active if the page is the current page in the navigation bar. However, all the attributs will be given the class active in this case. How do I fix this?
P.S: I am not looking for any JS or jQuery alternatives, I'm trying to make this work with PHP only.
You could use $_SERVER['SCRIPT_NAME'].
<ul class="topmenu">
<li <?php if($_SERVER['SCRIPT_NAME']=="/home.php") { ?> class="active" <?php } ?>><b>Bienvenue</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/livres.php") { ?> class="active" <?php } ?>><b>Livres</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/bibliotheque.php") { ?> class="active" <?php } ?>><b>Bibliothèque</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/universite.php") { ?> class="active" <?php } ?>><b>L'université</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/contact.php") { ?> class="active" <?php } ?>><b>Contact</b></li>
</ul>
I don't use PHP, but give the current page the active tag, and change it per file. Then a single class definition in the CSS handles changing the color for each page.
HTML:
<nav>
<a class="active" href="/home/">Home</a>
Calendar
Community
</nav>
CSS:
.active {
background-color: #4CAF50;
}
Declare page variables at the very top of each individual page.
Example: <? $page="home";?>
For each list item in your nav bar add if statment with corresponding variable and active class.
Example: <li class="<?if($page=="home"){?>active<?}?>"><b>Bienvenue</b></li>
What this does is assign each page a variable. The variable is compared in the if statements in the nav bar to determine which li gets the active class.
I'm using bootstrap 4 class. I've achieved Active Class selection something like this.
<li class="nav-item">
<a
<?php if ($_SERVER['SCRIPT_NAME'] == "/somepath/yourfile1.php") { ?>
class="nav-link active"
<?php } else { ?>
class="nav-link"
<?php } ?>
href="yourfile1.php">Home
</a>
</li>
<li class="nav-item">
<a
<?php if ($_SERVER['SCRIPT_NAME'] == "/somepath/yourfile2.php") { ?>
class="nav-link active"
<?php } else { ?>
class="nav-link"
<?php } ?>
href="yourfile2.php">Home
</a>
</li>
Repeat this logic for further li tags. Hope this helps someone else.
The code work correctly if you use "==" instead of "=" in The if construct
Еxample:
if($_GET['page'] **==** "home")...
Instead:
if($_GET['page'] **=** "home")...
Hope this helps someone else...
I have a ul li with parentid . I want all ul li with parentid but I'm getting multiple loops. The first steps and last are under same parentid.
<ul class="section-list">
<?php
if(count($outArrResults)>0){
for($i=0; $i<count($outArrResults); $i++){
?>
<li class="o-view">
<a href='javascript:void(0);'>
<h4 class="tit-section xsm">
<?php echo isset($outArrResults[$i]['cName']) ?$outArrResults[$i]['cName'] : '';?>
</h4>
</a>
<ul>
<?php
for($s=0; $s<count($outArrResults); $s++){
$contentSlugUrl = isset($outArrResults[$s]['conSName']) ? $outArrResults[$s]['conSName']:'';
$contentMenuTitle = isset($outArrResults[$s]['conMTitle']) ? $outArrResults[$s]['conMTitle']:'';
if($contentMenuTitle!=''){
?>
<li>
<a href='<?php echo $config['LIVE_URL'].'courses/'.$pAction1.'/'.$pAction2.'/'.$contentSlugUrl;?>'>
<span><?php echo $contentMenuTitle;?></span>
</a>
</li>
<?php
}
}
?>
</ul>
</li>
<?php
}
} else {
echo "No Results";
}
?>
So i have this in html
<ul>
<li>Home</li>
<li>Stuff</li>
<li>Other stuff</li>
</ul>
And i want to add this php code in the list:
<?php
...
elseif($_SESSION['logged']==false)
echo 'Login</li>';
echo 'Register</li>';
?>
But instead of displaying 2 separate list items, one with Login and one with Register i only get one with those two links.
What can I do?
You missed starting li tags and brackets.
do this:
<?php
...
elseif($_SESSION['logged']==false) {
echo '<li>Login</li>';
echo '<li>Register</li>';
}
?>
If $_SESSION['logged'] is going to be not set or false then use empty(), also your missed you opening li tags:
<ul>
<li>Home</li>
<li>Stuff</li>
<li>Other stuff</li>
<?php if(empty($_SESSION['logged'])): ?>
<li>Login</li>
<li>Register</li>
<?php endif; ?>
</ul>
You have forgotteh curly bracket { and li
<?php
...
elseif($_SESSION['logged']==false) {
echo '<li>Login</li>';
echo '<li>Register</li>';
}
?>
Hey guys i want the sensor to be hidden by default and it show when i click on the particular node..and should be the same on page reload
Here is my code
<ul >
<?php if(isset($nodes)): ?>
<?php $count = 0; ?>
<?php foreach($nodes as $node) { ?>
<?php $node_id=$node['node_id']; ?>
<?php $sensors = config_sensor_model::getsensors($node_id); ?>
<?php $count++; ?>
<li onclick="menu(<?php echo $count; ?>)"><?php echo $node['node_name']; ?> </li>
<ul id="<?php echo "sub_".$count; ?>">
<?php foreach($sensors as $sensorlog) { ?>
<li> <?php echo $sensorlog->sensor_name; ?></li>
<?php } ?>
</ul>
<?php } ?>
<?php endif; ?>
</ul>
</div>
this is the javascript presently i am using
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
function menu(count)
{
$("#sub_"+count).toggle("fast");
}
</script>
As mentioned in my comment just use CSS to hide the node initially like this:
<style type="text/css">
.hidden { display: none; }
</style>
<ul>
if(isset($nodes)):
$count = 0;
foreach($nodes as $node) {
$node_id = $node['node_id'];
$sensors = config_sensor_model::getsensors($node_id);
$count++;
?>
<li onclick="menu(<?php echo $count; ?>)"><?php echo $node['node_name']; ?> </li>
<ul id="<?php echo "sub_".$count; ?>" class="hidden">
<?php foreach($sensors as $sensorlog) { ?>
<li> <?php echo $sensorlog->sensor_name; ?></li>
<?php } ?>
</ul>
<?php }
endif; ?>
</ul>
</div>
When menu is clicked, the display value will be toggled by Javascript.
Set style='display:none' for the <ul>