Add automatic classes to each <li> - php

I have a list of < li >'s but without classes, I want to make for EACH < li > a different class.. something like this
< li class="1" >
< li class="2" >
< li class="3" >
Here is the code that I have:
<ul id="tralila">
<?php
foreach($lists as $key=>$region)
{
?>
<li>
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>

You can try:
<?php
$i = 1;
foreach($lists as $key=>$region)
{
?>
<li class="<?php echo "className$i"; ?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
$i++;
} ?>
Or, instead, you could use the $key from your foreach instead of creating, and incrementing, a counter variable.

So this is how you would do it using the key from $lists. Note I have added a prefix of li- as classes cannot start with a digit. See: Which characters are valid in CSS class names/selectors?
<ul id="tralila">
<?php
foreach($lists as $key=>$region)
{
?>
<li class="<?php echo 'li-' . htmlentities($key); ?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>
As all of the classes are unique I would use IDs instead of a class though.

Something like this?:
<ul id="tralila">
<?php
$class_name = 0;
foreach($lists as $key=>$region)
{
?>
<li class="<?=++$class_name;?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>

Related

For loop, runs once?

I'm using OpenCart and I'm trying to achieve rendering out the meta_description (used to identify the comic publisher) and use it to make a drop-down list with sub-categories, or to give the illusion of it. Here is my code now, it's an adopted version of the current OpenCart code. ['class'] is how I grab the child categories meta_description.
Basically, the second for statement doesn't work - it only does the first one. I would appreciated any kind of support on this.
<div class="menu">
<div id="top"></div>
<span>
<ul id="nav">
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if ($category['children']) { ?>
<div class="subs">
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<h3>DC Comics</h3>
<?php for (; $i < $j; $i++) { ?>
<?php if($category['children'][$i]['class'] == "DC Comics"){ ?>
<li>
<ul>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
<h3>Marvel</h3>
<?php for (; $i < $j; $i++) { ?>
<?php if($category['children'][$i]['class'] == "Marvel"){ ?>
<li>
<ul>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</div>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
</span>
</div>
Use different variables in loops, in you code $i is used in main loop and incremented in inner loops you can use foreach loop like ,
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if (is_array($category['children']) and isset($category['children'])) { ?>
<div class="subs">
<div>
<ul>
<?php
$li1='<li><h3>DC Comics</h3><ul>';
$li2='<li><h3>Marvel</h3><ul>';
foreach($category['children'] as $child)
{
if($child['class'] == "DC Comics")
{
$li1.='<li>'.$child['name'].'</li>';
}
if($child['class'] == "Marvel")
{
$li2.='<li>'.$child['name'].'</li>';
}
}
$li1.='</ul></li>';
$li2.='</ul></li>';
echo $li1;
echo $li2;
?>
</ul>
</div>
</div>
<?php } ?>
</li>
<?php } ?>

menu by default should hidden

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>

create new ul after every 13 li elements

First of all, I don't know how to count in php, maybe someone could recommend me a good source to read;
Second, I'm not asking to solve this for me, but I just want a hint or simpler explanation that would make sense;
Here is my function:
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<p><?php echo $category['name']; ?></p>
<?php if ($category['children']) { ?>
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
I want this part, every 13 li elements to create a new .ul. ./ul. tags
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<ul><li>maximum of 13 elements</li></ul>
after 13 <li></li> elements create new <ul></ul> tags and put the 14 <li></li> element into the new <ul></ul> tag
I hope I explained what I want to do, for now I'll be waiting for your answers,
p.s. this is more for my learning skills then actual work, so thanks in advice
It's simple. Use condition
if ($wi && $wi % 13 == 0) {
print '</ul><ul>';
}
In order to count 13 elements, you can use the modulo operator %. The idea is that ($iw % 13) is equal to 0 if the content of $iw is a multiple of 13. (see more about the modulo operation)
With an if statement you can insert </ul><ul> when you need in your for loop.
There's two possibilities I can think of:
you could have a separate counter wich resets to 1 and outputs a <ul> every time it reaches 13
or you could check each time if $wi id divisible by 13:
<?php
for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) {
if ($wi && $wi % 13 == 0) {
// output <ul>
}
}
?>
If you're unsure what % means, here's the appropriate PHP manual page

Foreach loop and incrementing variable and output to css

I use Kirby CMS as backend.
I want following structure for my html output:
<ul>
<li class="link-1">Link</li>
<li class="link-2">Link</li>
<li class="link-3">Link</li>
<li class="link-4">Link</li>
</ul>
I have following code:
<?php foreach($pages->visible() AS $p): ?>
<?php $nbr = $pages->countVisible()?>
<li class="link-<?php for ($i = 1; $i <= $nbr; $i++){echo $i;} ?>">
<a<?php echo ($p->isOpen()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo html($p->title()) ?></a></li>
<?php endforeach ?>
But instead I only get the css class
link-1234
in each of the links, so it is making the for loop, but I need only one number per foreach loop.
This code made it work:
<li class="link-<?php static $x=1; echo $x; $x++; ?>">
<li class="link-<?php for ($i = 1; $i <= $nbr; $i++){echo $i;} ?>">
only loops inside that element
<?php for ($i = 1; $i <= $nbr; $i++){
echo "<li class=\"link-$i\">";
echo 'the rest of the line';
} ?>
should loop the whole block

how to change values between loop

I extracted the values of the array on the foreach loop, but I want when it retrieves a certain 'br_title' changed href link to <a href="http://noorresults.moe.sa..how to do that with simple code?..
<ul class="riMenu">
<?php foreach ($last_branches as $last) { ?>
<li> <?php if (!empty($last['br_title'])) echo $last['br_title'] ?></li>
<?php }; ?>
</ul>
</div>
</div><!-- Menu E
You can change your code as below
<?php
$total_to_show = 5; // take offset
$count = 1; //counter until it reach to offset
foreach ($last_branches as $last) { if($count > $total_to_show){?>
<li> <?php if(!empty($last['br_title'])) echo $last['br_title'] ?></li>
<? }else{?>
<li> <?php if(!empty($last['br_title'])) echo $last['br_title'] ?></li>
<?php }
$count++;
} ?>

Categories