PHP Using single Count++ on both loop/conditional & static content? - php

Is it possible to use count++ and print it without conditionals or loop?
Normally I use it on conditionals/loop like example:
<?php $count = 1; { while( have_rows('test') ): the_row(); ?>
Text <?php echo $count;?>
<?php $count++;?>
<?php endwhile; }?>
But I want it to use it in stuff like this:
<?php $count = 1; {?>
Static Text <?php echo $count;?>
Static Text <?php echo $count;?>
<?phpwhile( have_rows('test') ): the_row(); ?>
Loop Text <?php echo $count;?>
<?php endwhile;?>
<?php $count++;?>
<?php}?>
How to make the counting work on both static & loop contents?

Related

Use $I++ more than once in a loop

I need to use the value for $i++ in numerous places but if I do that then some fields end up skipping values and instead of being 1, 2, 3 etc. they are 1, 3, 5 and the other field has values of 2, 4, 6 etc.
<?php $i = 1; ?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i++; ?></div>
<div>Should be the same as this one: <?php echo $i++; ?></div>
<?php end while; ?>
It works if I create another variable but this feels like a hack.
<?php $i = 1;
$x = 1;
?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i++; ?></div>
<div>Should be the same as this one: <?php echo $x++; ?></div>
<?php end while; ?>
Is there a better way?
You can do the increment as a discrete step at the end of the loop, this means you can use it in various places without wondering when you changed the value...
<?php $i = 1; ?>
<?php while (have_rows( 'something' ) ):
the_row(); ?>
<div>This number: <?php echo $i; ?></div>
<div>Should be the same as this one: <?php echo $i; ?></div>
<?php $i++; ?>
<?php end while; ?>
The solution is simple - increase $i only once:
<?php $i = 1; ?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i; ?></div>
<div>Should be the same as this one: <?php echo $i++; ?></div>
<?php end while; ?>
A clean approach would be to echo the value of $i in your loop, and only increase it at the end, as a step alone. This is a good approach to have not only in PHP but in programming in general.
<?php $i = 1; ?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i; ?></div>
<div>Should be the same as this one: <?php echo $i; ?></div>
<?php
$i++;
end while; ?>

Adding div class based on PHP variable from ACF repeater field?

I am looping through a repeater field in Advanced Custom Fields and displaying divs for each item in the repeater. If the index is equal to 0, I want to add a special class to just that div. Is this possible? Here's what I've tried:
<?php if (have_rows('products')): $i = 0; ?>
<div class="product-container">
<?php while (have_rows('products')) : the_row(); ?>
<div class="product <?php if ($i == 0) { echo 'active-class'; } ?>"></div>
<?php $i++; endwhile; ?>
</div>
<?php endif; ?>
Unfortunately this is not working.
Instead of doing the conditional within the class, I just did it on the outside and defined two different class tags based on the condition:
<?php if (have_rows('products')): $i = 0; ?>
<div class="product-container">
<?php while (have_rows('products')) : the_row(); ?>
<div <?php if ($i == 0) { ?>class="product active"<?php } else { ?>class="product"<?php } ?> ></div>
<?php $i++; endwhile; ?>
</div>
<?php endif; ?>

Wordpress Loop For Even/Odd Posts

I have two columns and I want one post type to get distributed to each column evenly. So it’s two side-by-side divs and I want:
Div1 = Post1, Post3, Post5
Div2 = Post2, Post4, Post6
So basically get odd/even posts. Not exactly sure how to do it.
<?php query_posts('post_type=post-type'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="column1">
<?php
//Get Odd Posts
?>
</div>
<div class="column2">
<?php
//Get Even Posts
?>
</div>
<?php endwhile; ?>
<?php else : ?>
//Something that happens when a post isn’t found.
<?php endif; ?>
You want to use the modulus operator something like this:
<?php
$i = 0;
for ($i = 0; $i <20; $i++){
$class = $i % 2 == 0 ? "even" : "odd";
echo "<div class='" . $class . "'>";
echo "</div>";
}
?>
To do what you want, first you have to store the results somewhere (as even/odd), then display them.
Though you should really target these posts with CSS, not PHP, as it's hackish at best.
<?php query_posts('post_type=post-type'); ?>
<?php if (have_posts()) : ?>
<?php
$i = 0;
while (have_posts())
{
$key = $i & 1 ? 'odd' : 'even';
$post[$key] = array(get_the_title() => get_the_content());
$i++;
}
?>
<div class="column1">
<?php foreach ($post['even'] as $title => $content) : ?>
<?php echo $title; ?>
<?php echo $content; ?>
<?php endforeach; ?>
</div>
<div class="column2">
<?php foreach ($post['odd'] as $title => $content) : ?>
<?php echo $title; ?>
<?php echo $content; ?>
<?php endforeach; ?>
</div>
<?php else : ?>
//Something that happens when a post isn’t found.
<?php endif; ?>

Looping through database and returning relevant data

In my controller I have this Code to loop through database and return the data
$faultgroup = $this->booking_model->Get_Fault_Group_Display($grouptype);
$data['Get_Fault_Group_Display'] = $faultgroup; $getresults = array();
$data['get_fault_group_data'] = array();
foreach ($faultgroup as $key ) {
$show = $key->Showgroup;
$getresults = $this->booking_model->get_fault_group_data($grouptype,$show);
$data['get_fault_group_data'] = $getresults ;
}
In my View i have this Code to loop through each record with the specific grouptype and display record (to_do_item) from database that match that grouptype
<?php if ( ! is_null($Get_Fault_Group_Display)): ?>
<?php if (count($Get_Fault_Group_Display)): ?>
<?php foreach ($Get_Fault_Group_Display as $result): ?>
<?php echo $result->Showgroup; ?>
<?php foreach ($get_fault_group_data as $key) :?>
<?php echo $key->to_do_item; ?>
<?php endforeach ?>
<?php endforeach ?>
<?php else: ?>
<?php endif ?>
My problem is only the last row is shown on all the grouptypes because the loop keeps overiding $data['get_fault_group_data'] with the new $getresults
Shouldn't you use the $data['get_fault_group_data'] as an array?
Controler:
$data['get_fault_group_data'][$key] = $getresults ;
View:
<?php if ( ! is_null($Get_Fault_Group_Display)): ?>
<?php if (count($Get_Fault_Group_Display)): ?>
<?php foreach ($Get_Fault_Group_Display as $i => $result): ?>
<?php echo $result->Showgroup; ?>
<?php foreach ($get_fault_group_data[$i] as $key) :?>
<?php echo $key->to_do_item; ?>
<?php endforeach ?>
<?php endforeach ?>
<?php else: ?>
<?php endif ?>

Remove "," at end of foreach loop

I've created a loop to list a set of meta values. I've been able to apply a class to the last item in the list, but I'd like to remove the "," at the end of the last value. Any help would be much appreciated.
<?php $count = count($subcategory); $num = 0; ?>
<?php foreach ($subcategory as $subcategory): ?>
<p
<?php if($num == $count-1){ ?>
class="subcategory-item subcategory-last-item inline-block"
<?php } ?>
class="inline-block subcategory-item"> <?php echo $subcategory;?>,</p>
<?php $num++ ?>
<?php endforeach; ?>
I may be taking an incorrect route by worrying about adding a class to the last item. If I can remove the "," from the last item I'll be happy.
Here's a quick rewrite which may lead you to a solution:
<?php $count = count($subcategories); $num = 0; ?>
<?php $classes = 'inline-block subcategory-item'; ?>
<?php foreach ($subcategories as $subcategory): ?>
<p class="<?=$classes.($num==$count-1?' subcategory-last-item':'')?>">
<?php echo $subcategory;?>
<?php if ($num<$count-1): ?>
,
<?php endif; ?>
</p>
<?php $num++ ?>
<?php endforeach; ?>

Categories