Foreach loop and incrementing variable and output to css - php

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

Related

How can I echo 'for' loop here

I want to use $i into his PHP condition how can I do this? I have no idea how to echo into condition. plz see the code and if you can plz help me. thank you advanced.
<?php
for($i=1; $i<4; $i++) {
if($top_h_text_**I want to use $i here!**) {
?>
<li>
<?php
if($top_h_icon_**I want to use $i here!**) {
?>
<i class="<?php echo $top_h_icon_**I want to use $i here!**;?>"></i>
<?php
}
if($top_h_icon_**I want to use $i here!** == 'fa fa-envelope'){
?>
<span class="top_header_text"><?php echo $top_h_text_**I want to use $i here!**;?></span>
<?php
} else {
?>
<span class="top_header_text"><?php echo $top_h_text_**I want to use $i here!**;?></span>
<?php
}
?>
</li>
<?php
}
}
?>
To accomplish what you are trying to do, simply follow this example:
$top_h_text_**I want to use $i here!**
becomes
${"top_h_text_".$i}
So you basically take the string you want (which should be the name of an existing variable) and wrap it with ${}
If this is in fact WordPress, and your top_h_icon_ and top_h_text are in fact defined. This would be appropriate method for concatenation of your $i increment and the variables. This is how you join two parts to make a single string.
Note that with WordPress and outputting dynamic variables, you should escape them.
<?php
for ( $i = 1; $i < 4; $i++ ) {
if ( $top_h_text_ . $i ) {
?>
<li>
<?php
if ( $top_h_icon_ . $i ) {
?>
<i class="<?php echo esc_attr( $top_h_icon_ . $i ); ?>"></i>
<?php
}
if ( 'fa fa-envelope' === $top_h_icon_ . $i ) {
?>
<span class="top_header_text"><?php echo esc_attr( $top_h_text_ . $i ); ?></span>
<?php
} else {
?>
<span class="top_header_text"><?php echo esc_attr( $top_h_text_ . $i ); ?></span>
<?php
}
?>
</li>
<?php
}
}

Combine for and if statements

I've been researching if this is possible, but I've drawn a blank, I'm wondering if it's possible to optimize these for and if statements together? cheers.
Edit (Updated #2) : Their is an issue with the code not looping through the pages.
<?php
// calculate total number of pages
$total_pages = ceil($total_rows / $records_per_page);
// range of links to show
$range = 2;
// display links to 'range of pages' around 'current page'
$initial_num = $page - $range;
$condition_limit_num = ($page + $range) + 1;
?>
<ul class="pagination margin-zero">
<?php if ($page>1) : ?>
<li>
<a href='<?php echo $page_url; ?>page=1' title='Go to the first page.'>First Page</a>
</li>
<?php endif; ?>
<?php
for ($x = min($initial_num, 0); $x <= max($condition_limit_num-1, $total_pages); $x++) :
if ($x == $page) :
?>
<li class='active'>
<?php echo $x; ?> <span class="sr-only">(current)</span>
</li>
<?php else : ?>
<li>
<a href='<?php echo $page_url; ?>page=<?php echo $x; ?>'><?php echo $x; ?></a>
</li>
<?php
endif;
endfor;
?>
<?php
if ($page<$total_pages) : ?>
<li>
<a href='<?php echo $page_url; ?>page=<?php echo $total_pages; ?>' title='Last page is <?php echo $total_pages; ?>'>
Last Page
</a>
</li>
<?php endif; ?>
</ul>
Of course you can combine them if you think about the maximum and minimum value that $x is allowed to get assigned.
The outer for loop would require $x to be contained in the interval [0; $condition_limit_num)
If you look only at the for and the first if you could decide that the minimum has to be larger than zero or initial_num, so you could for example use the minimum of initial_num and zero. The limit would work the same way using the maximum of condition_limit_num-1 and total_pages as uppermost reachable value, i.e. the interval [min($x, 0); max($condition_limit_num-1;$total_pages)].
But if you take into account the innermost if you require $x to have a specific value ($page). That means that this if is only "true" whenever $page is contained in the intervall [min($x, 0); max($condition_limit_num-1;$total_pages)] - you can reduce that check to a single if.
Update after question update:
Since the innermost if also has an else path the loop cannot be reduced to a single if:
<?php
for ($x = max($initial_num, 0); $x <= min($condition_limit_num-1, $total_pages); $x++)) :
if($x == $page) :
?>
<li class='active'>
<?php echo $x; ?> <span class="sr-only">(current)</span>
</li>
<?php else : ?>
<li>
<a href='<?php echo $page_url; ?>page=<?php echo $x; ?>'><?php echo $x; ?></a>
</li>
<?php
endif;
endfor;
?>

Generating increasing number

I Have a ordered list and i want to generate a incemental number for the data-slide-to starting from 0
That is the php code
get("display_indicators", 1)): ?>
$item){
$activeclass = "";
if($key == 0){
$activeclass = "active";
}
?>
id;?>" data-slide-to="" class="">
You could use a variable to contain a counter...
<?php $counter = 0; ?>
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="<?php
echo $counter;
$counter++;
?>" class="<?php echo $activeclass; ?>"></li>
You could even use the following instead of echo-ing and incrementing on two lines...
echo $counter++;
This echoes the current count and increments afterwards, but some people prefer to avoid this.
Try this, hope it'll help you
<?php $uniqueNo=0; ?>
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="
<?php echo $uniqueNo+=1; ?>" class="<?php echo $activeclass; ?>"></li>
Use an incremential counter
<?php $counter = 0; ?>
<ul>
enter code here
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="<?=$counter++?>"
class="<?php echo $activeclass; ?>"></li>
this however could not be "unique" application wide

Add automatic classes to each <li>

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>

how to add big space

<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
the above code is used to output an article list. now i want to add big space after the lines which is the multiple of 10.namely,add a big space (eg:margin-bottom:30px,only to after every 10 bullet points but the space between other li is 15px )after every 10 bullet points. how to change the above code. then i can use css to get that.
–
Although I don't have a clue what you mean by 'big space', I assume you want to place an arbitry HTML tag after every 10th list item. You can do this like so, for example:
<?php $i = 0; ?>
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php if ($i++ === 10): ?><br /><?php $i = 0; endif; ?>
<?php endforeach; ?>
EDIT, you clarified:
no,eg:30px, but the space between other <li> is 20px.
Which can be done like so:
<?php $i = 1; ?>
<?php foreach ($rows as $id => $row): ?>
<?php $i++; ?>
<li style="margin-bottom:<?php echo ($i === 10) ? : '30' : '20'); ?>px;" class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php if ($i === 10) $i = 1; ?>
<?php endforeach; ?>
The modulus operator (%) is ideal for this:
<?php $i = 0; foreach ( $rows as $id => $row ): ?>
<li style="margin-bottom: <?php echo $i ++ % 10 ? '30' : '20' ?>px;" class="<?php echo $classes[$id] ?>">
<?php echo $row ?>
</li>
<?php endforeach ?>
As a slight amendment to Aron's code, if you wanted to use CSS to style the <li>:
<?php $i = 1;
foreach ($rows as $id => $row):
if($i === 10){
$end_class = ' end-class';
$i = 1;
} else {
$end_class = '';
$i++;
}
echo '<li class="' . $classes[$id] . $end_class . '">' . $row . '</li>';
endforeach;
?>
You can then apply the style to .end-class.

Categories