ACF / PHP Repeater count - php

I have a repeater using Advanced Custom fields, which when a count reaches a total (of 3) based on an iterative count, I would like to output a div, then reset the counter.
Here's the code, at the moment it's not outputting as it should and I don't know enough about math to get it to work.
Appreciate your assistance :-)
<?php if(have_rows('flexible_row')) : ?>
<div class="row">
<?php if(empty($count)){ $count=1;} while(have_rows('flexible_row')) : the_row(); ?>
<?php if(get_sub_field('column_width') == "One column") { $count = $count+1; ?>
<div class="col-sm-4">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $count; ?>
<hr/>
</div>
<?php } if(get_sub_field('column_width') == "Two columns") { $count = $count+2; ?>
<div class="col-sm-8">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $counter; ?>
<hr/>
</div>
<?php } else { $count = $count+3; ?>
<div class="col-sm-12">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $counter; ?>
<hr/>
</div>
<?php } if ($count == 3) { ?></div><div class="row"><?php $count = 0; } ?>
<?php endwhile; ?>
</div>
<?php endif; ?>

Think I'd got my logic mixed up a bit — I wasn't using the else clauses correctly. Here's the final working code in case anyone stumbles across this in the future:
<?php if(have_rows('flexible_row')) : ?>
<div class="row">
<?php if(empty($count)){ $count=0;} while(have_rows('flexible_row')) : the_row(); ?>
<?php if(get_sub_field('column_width') == "One Column") { $count = $count+1; ?>
<div class="col-sm-4">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } elseif(get_sub_field('column_width') == "Two Columns") { $count = $count+2; ?>
<div class="col-sm-8">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } else { $count = $count+3; ?>
<div class="col-sm-12">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } if ($count == 3) { ?></div><div class="row"><?php $count = 0; } ?>
<?php endwhile; ?>
</div>
You could of course use switch statements too..

Related

Running three iterations on a foreach loop

I am running a listing on a page in a row with three columns each. Each column requires different classes, I need to find a way to return these classes in a foreach loop to achieve the following ...
<div class="row">
<div class="box_skin1">...</div>
<div class="box_skin2">...</div>
<div class="box_skin3">...</div>
<div class="box_skin1">...</div>
<div class="box_skin2">...</div>
<div class="box_skin3">...</div>
</div>
I am currently using the following for alternating rows but now need to add a third ... how do I do that?
<?php $counter = 1; ?>
<div class="row">
<?php foreach ($newsRecords as $record): ?>
<?php if($counter % 2) : ?>
<!-- ROW 1 -->
<div class="box_skin1">...</div>
<?php else : ?>
<!-- ROW 2 -->
<div class="box_skin2">...</div>
<?php endif; ?>
<?php $counter++; ?>
<?php endforeach ?>
</div>
This should work
EDITED
<?php $counter = 1; ?>
<div class="row">
<?php foreach ($newsRecords as $record): ?>
<?php if($counter % 3 == 0) : ?>
<!-- ROW 3 -->
<div class="box_skin3">...</div>
<?php elseif ($counter % 3 == 2) : ?>
<!-- ROW 2 -->
<div class="box_skin2">...</div>
<?php elseif ($counter % 3 == 1): ?>
<!-- ROW 1 -->
<div class="box_skin1">...</div>
<?php endif; ?>
<?php $counter++; ?>
<?php endforeach ?>
</div>
IF-ELSE free structure. If you plan to add new div then you dont have to add new else-if condition just you need to increase counter size and it will work.
<?php $counter = 1; ?>
<div class="row">
<?php foreach ($newsRecords as $record): ?>
<div class="box_skin<?php echo $counter; ?>">...</div>
<?php
$counter++;
if ($counter >= 3) {
$counter = 1;
}
?>
<?php endforeach ?>
</div>

PHP logic to add a class to 2 rows every other 2 rows

Can anyone help me with the logic to add a class .col-md-offset-2 to every two rows in my code below...
<?php while(have_rows('report_quotes')): the_row(); ?>
<div class="col-md-12 <?php // help me ?>">
<?php /* quote */ ?>
<?php Post::get_template_part('sections/quote'); ?>
</div>
<?php endwhile; ?>
So the output will look like this...
<div class="col-md-12">...</div>
<div class="col-md-12">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
<div class="col-md-12">...</div>
<div class="col-md-12">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
<div class="col-md-12">...</div>
<div class="col-md-12">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
...
Can anyone help?
Thanks
You can do this by using the modulus operator (%)
<?php $i = 0; while($i < 6): ++$i; ?>
<div class="col-md-12 <?=in_array($i % 4, array (0,3)) ? 'col-md-offset-4' : null ?>">...</div>
<?php endwhile; ?>
So in your case -
<?php $i = 0; while(have_rows()): the_row(); ++$i; ?>
<div class="col-md-12 <?=in_array($i % 4, array (0,3)) ? 'col-md-offset-4' : null ?>">
<?php /* quote */ ?>
<?php Post::get_template_part('sections/quote'); ?>
</div>
<?php endwhile; ?>
This essentially gets the remainder for each increment and in our case we know that if the remainder is 0 (divisible by 4) or a 3, then it should be indented.
Add a pointer that keeps track of the current loop index, and check if it the 3th or the 4th one in an iteration with the modulo operator.
Like this:
<?php
$i = -1;
while(have_rows('report_quotes')): the_row();
$i++;
?>
<div class="col-md-12
<?php if ( ($i % 4) == 2 || ($i % 4) == 3 ) { echo 'col-md-offset-2'; } ?> ">
<?php /* quote */ ?>
<?php Post::get_template_part('sections/quote'); ?>
</div>
<?php endwhile; ?>
Add a counter to know which row of the array you're in and use modulo:
<?php
$i = 0;
while(have_rows('report_quotes')): the_row(); ?>
<div class="col-md-12 <?php if (($i%4==2)||($i%4==3) {echo ' col-md-offset-2'; } ?> ?>">
<?php Post::get_template_part('sections/quote'); ?>
</div>
<?php endwhile; ?>

Put else statement that is inside a while loop into one div

I have a while loop and inside the while loop there is an if-else statement. All I want to do is put all the contents of the else statements into one div. It's in Wordpress.
I can't seem to get the logic behind it..
<?php /* The loop */
$i = 1;
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
static $count = 0;
if ($count == 5) { break; }
else {
?>
<div class="content_container">
<?php
if($i == 1) {
?><span style="width:370px;" class="main active"><?php the_post_thumbnail('full');?></span><?php
} else {
?><span class="child nonactive"><?php the_post_thumbnail('full'); ?></span><?php
}
$i++;
$count++;
?>
</div>
<?php
}
?>
<?php endwhile; ?>
<div class="content_container">
<span <?php if($i == 1) { ?> style="width:370px;" class="main active" <?php } else {?> class="child nonactive" ><?php the_post_thumbnail('full');?></span>
<?php }
$i++;
$count++;
?>
</div>

Find last row in a foreach query

How could I find the last post and NOT put <hr> below it?
<?php foreach($db->query("SELECT * FROM news") as $row): ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<hr>
<?php endforeach ?>
Set a counter equal to 1 which increments each round, and check if it is equal to the count() of the $query array returned. If it is, you are on the last round!
<?php
$counter = 1;
$total = count($db->query("SELECT * FROM news"));
?>
<?php foreach($db->query("SELECT * FROM news") as $row): ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<?php if($counter != $total){
?>
<hr>
<?php
}
?>
<?php $counter++; ?>
<?php endforeach ?>
A solution would be to build an array and then use join :
<?php
$arr=array();
foreach($db->query("SELECT * FROM news") as $row):
$arr[] = '<div class="NewsHeadline"><div class="NewsDate">'.$date
.' - </div><div class="NewsTitle">'.$title'
.</div></div><div class="NewsBody">'.$body.'</div>';
endforeach
echo join("<hr>",$arr);
?>
You can convert to regular for loop and then compare $i to the size of your query result:
<?php
$news = $db->query("SELECT * FROM news");
for($i=0; $i<sizeof($news);$i++){ ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<?php if(($i+1) < sizeof($news)) echo '<hr>'; ?>
<?php } ?>
If you get a count of the rows prior to the loop, you can check the count and avoid adding the HR.
$rows = $db->query("SELECT * FROM news");
$row_count = $rows.count();
$iter = 0;
foreach($rows as $row)
{
//...
if ($iter < $row_count)
{
// render hr
}
$iter++;
}
You could also go with a pure CSS approach:
div.containerClass:last-child hr { display:none; }
where "containerClass" would be whatever element your posted code sits inside of.

While-loop: group two result on one div

I would add each two result one div class
<?php while ($fetch = $db->fetch($query)) { ?>
<?php echo $fetch['title']; ?>
<?php } ?>
Output should be like this
<div class="one">
<div class="two">
<article>Title</article>
<article>Title</article>
</div>
</div>
<div class="one">
<div class="two">
<article>Title</article>
<article>Title</article>
</div>
</div>>
<?php
$i=0;
while ($fetch = $db->fetch($query)) { ?>
<?php if ($i%2==0) { ?>
<div class="one">
<div class="two">
<?php } ?>
<article><?php echo $fetch['title']; ?></article>
<?php if ($i++%2==1) { ?>
</div>
</div>
<?php } ?>
<?php } ?>
//Also is a good idea to verify if the <div> tags are closed
<?php if ($i%2==1) { ?>
</div>
</div>
<?php } ?>
$count = 0;
while ($fetch = $db->fetch($query))
{
if ($count == 0)
echo '<div class="one"><div class="two">';
if ($count < 2)
echo '<article>'.$fetch['title'].'</article>';
if ($count == 2)
{
echo '</div></div>';
$count = 0;
}
$count++;
}

Categories