PHP loop add div around items between a count - php

I'm using the following working code to wrap every 3 elements in a div:
$count = 1
foreach( $names as $name ):
if ($count%3 == 1) {
echo '<div class="wrap">';
}
echo $name;
if ($count%3 == 0) {
echo '</div>';
}
$count++;
endforeach;
if ($count%3 != 1) echo "</div>";
That returns:
<div class="wrap">
name
name
name
</div>
<div class="wrap">
name
name
name
</div>
<div class="wrap">
name
name
name
</div>
<div class="wrap">
name
name
name
</div>
So far so good.. but i want the second wrapped set to have 4 "name" elements like so:
<div class="wrap">
name
name
name
</div>
<div class="wrap">
name
name
name
name
</div>
<div class="wrap">
name
name
name
</div>
<div class="wrap">
name
name
name
</div>
Every 3 items should be wrapped in the div, except the second set which will have 4 items.
Or another way to explain: items 4 to 8 will be wrapped in a div while every other 3 items will be wrapped in div.
How can this be achieved?

Add special cases for the first two DIVs, and adjust the modulus for the ones after that.
$count = 1
foreach( $names as $name ):
if ($count == 1 || $count == 4 || ($count > 5 && $count % 3 == 2)) {
echo '<div class="wrap">';
}
echo $name;
if ($count == 3 || $count == 7 || ($count > 7 && $count % 3 == 1)) {
echo '</div>';
}
$count++;
endforeach;
// Finish the last block -- lots of different cases
if ($count < 4 || ($count > 4 && $count < 8) || ($count > 8 && $count % 3 != 2)) {
echo '</div>';
}

Related

Wordpress - Split custom posts into rows and columns

I have a layout where we want to place the custom posts types on alternating rows and 2 & 3 columns.
so basically:
<div class="wrapper">
<div class="row">
<div>post 1</div>
<div>post 2</div>
</div>
<div class="row">
<div>post 3</div>
<div>post 4</div>
<div>post 5</div>
</div>
<!-- then 2 new row with 2 columns followed by 3 columns and so on -->
</div>
Any suggestions?
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
);
$the_query = new WP_Query($args);
if (!empty($the_query->posts)) {
$count_post_row = 0;
$rowcount = 1;
echo '<div class="wrapper">'; // start wrapper
foreach ($the_query->posts as $post) {
$count_post_row++;
if ($count_post_row == 1) {
echo '<div class="row">'; // start row
}
echo '<div>' . $post->post_title . '</div>';
if ($rowcount == 1 && $count_post_row == 2) {
$count_post_row = 0;
$rowcount = 2;
echo '</div>'; // end row if its first section and 2 post add
}
if ($rowcount == 2 && $count_post_row == 3) {
$rowcount = 1;
$count_post_row = 0;
echo '</div>'; // end row if its second section and 3 post add
}
}
echo '</div>'; // start wrapper
}
This is the code you got your results and its continue as your requirements you can check the screenshot below.. If need any more help drop the comment or message will help..

Check If bootstrap last column is even or odd

I have bootstrap column in while loop, what I want is if bootstrap last column is odd then I want column to be 12 (col-12), I found the way to check number even or odd but want to check last number so if last number (last column) is odd I want column to be 12 else remain col-6
I have tried:
<div class"<?php echo ($i == (2 || 4 || 6) )?'col-md-6':'col-md-12'; ?>">
display content there in while loop
</div>
<div class"<?php echo ($i % 2 == 0)?'col-md-6':'col-md-12'; ?>">
display content there in while loop
</div>
but could get the job done...
You make a mistake in writing HTML. But version with modulo must work:
<div class="row">
<?php for ($i = 0; $i < 100; $i++) { ?>
<div class="<?php echo ($i%2 === 0) ? 'col-md-6' : 'col-md-12'; ?>">
display content there in while loop
</div>
<?php } ?>
</div>
solution By m_hutley
Where $i = counter;
$total = acf repeater count;
<div class="<?php echo ($i == $total && $total % 2 !== 0 )?'col-md-12':'col-md-6'; ?>" id="<?php echo $i; ?>">
<h1><?php echo "contain"; ?></h1>
</div>

Different layout for nth rows ACF REPEATER

I am using ACF repeater to display images, I want to achieve layout so that 1 - 2 - 3 elements go with col-lg-4 grid and 4-5-6-7 go with col-lg-3 grid and so on, repeating that layout for all items
I've tried using but it's to get 3 elements into 1 div
mostly my layout will be
first row 3x col-lg-4
second row 4x col-lg-3
third row 3x col-lg-4
fourth row 4x col-lg-3
<?php
// check if the repeater field has rows of data
if( have_rows('gallery_repeater') ):
// loop through the rows of data
// add a counter
$count = 0;
$group = 0;
while ( have_rows('gallery_repeater') ) : the_row();
// vars
$teacher_bio = get_sub_field('image');
if ($count % 3 == 0) {
$group++;
?>
<div id="teachers-<?php echo $group; ?>" class="cf group-<?php echo $group; ?>">
<?php
}
?>
<div class="teacher">
<img src="<?php the_sub_field('image'); ?>" />
<?php echo $teacher_bio; ?>
</div><!-- .teacher -->
<?php
if ($count % 3 == 2) {
?>
</div><!-- #teachers -->
<?php
}
$count++;
endwhile;
else :
// no rows found
endif;
?>
Hi Please check below code for reference :
$gallery_repeater = get_field('gallery_repeater');
foreach (array_chunk($gallery_repeater, 7) as $key => $value) {
foreach ($value as $k => $val) {
$class = $k < 3 ? 'col-lg-3' : 'col-lg-4';
echo '<div class="'.$class.'"> <img src="'.$val['image'].'" />'.$val['teacher_bio'].'</div>';
}
}

Conditions with PHP

I'm still pretty new to PHP and trying to figure out an issue I've found on our website at work. In the screenshot, the last div.image-group should be inside the div.image-row that only has two items in it — basically, I always want the div.image-group to be inside a div.image-row, which should hold at most three div.image-group's (but the first div.image-row might contain fewer).
Can anyone point me in the right direction to correct this?
Here's the code:
<?php $counter = 0; ?>
<?php foreach ($rows as $id => $row): ?>
<?php $counter++; ?>
<?php if ($counter % 3 == 1 || $counter === 1) { ?>
<div class="image-row small clearfix">
<?php } ?>
<div class="image-group"><?php print $row; ?></div>
<?php if (($counter != 1 && $counter % 2 == 1) || ($id == count($rows) -1 && $counter % 2 != 1)) { ?>
</div>
<?php } ?>
<?php endforeach; ?>
The code will be simpler if you divide the original array into groups of three at the beginning.
<?php foreach (array_chunk($rows, 3) as $image_row): ?>
<div class="image-row small clearfix">
<?php foreach ($image_row as $image_group): ?>
<div class="image-group"><?= $image_group ?></div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
The second condition should be:
<?php if ($counter % 3 == 0 || $counter == count($rows)) { ?>
$counter % 3 == 0 is true after every third row, and $counter == count($rows) is true after the last row.

How Can I fillup bootstrap grid using foreach?

As I am trying to make bootstrap grid structure using foreach two column and inside it's one row and two rows structure. Attached a screen shot for better to better view.
Note: Not issue with CSS but first three items are goes in grid as picture.
Counter is not taking col-md-4 and not sure where to use Counter increment counter++? What if I want to show first three items using foreach?
Any other way to workaround on this layout? Tried so far as below
<?php $counter = 0;
foreach($view->result as $result) {
<div id="hot-post" class="row hot-post">
<?php if( $counter == 0 || $counter % 3 == 0 ) { ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body
</div>
</div>
<?php if( $counter == 1 || $counter == 2 ) { ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter == 1){ ?>
<div class="post post-thumb">
// second element common body
</div>
</php if ( $counter == 2){
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php counter++; } } ?>
</div> //closing row
<?php } ?> //closing foreach
There are couple of syntax errors in your code, such as:
Opening and closing braces are not in sync i.e. foreach loop and if block(s) are not closed properly
counter++; should be $counter++;
Plus, there are few logical errors as well. Change your code in the following way,
<?php
$counter = 0;
foreach($view->result as $result) {
?>
<div id="hot-post" class="row hot-post">
<?php if( $counter == 0 || $counter % 3 == 0 ) { ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body
</div>
</div>
<?php } ?>
<?php if( $counter != 0 && $counter % 3 != 0 ) { ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter % 3 == 1){ ?>
<div class="post post-thumb">
// second element common body
</div>
<?php } ?>
<?php if ( $counter % 3 == 2){ ?>
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php } ?>
</div>
<?php
}
++$counter;
}
?>

Categories