Wrapping a Div Around Every Third Item in a Foreach Loop PHP

Wrapping a div around every third item in a foreach loop PHP

You need to have a separate counter variable:

$i = 0;
foreach($info as $key => $val){
if($i%3 == 0) {
echo $i > 0 ? "</div>" : ""; // close div if it's not the first
echo "<div class='container'>";
}
?>
<div class="holder">
<div class="name">
<?php echo $Name ?>
</div>
<div class="colour">
<?php echo $colour ?>
</div>
</div>
<?php
$i++;
}
?>
</div> <!-- close last container div -->

php foreach loop wrap every 2 elements, then every 3 elements

Use nested loops. The outer loop counts by 5, then two inner loops that print two elements and then three elements.

$len = count($list);
for($i = 0; $i < $len; $i += 5) {
echo '<div class="row double">';
for ($j = $i; $j < min($len, $i+2); $j++) {
echo '<div class="col-lg-6 col-12">' . $list($j) . '</div>';
}
echo '</div>';
if ($j >= $len) {
break;
}
echo '<div class="row triple">';
for (; $j < min($len, $i + 3); $j++) {
echo '<div class="col-lg-4 col-12">' . $list($j) . '</div>';
}
echo '</div>';
}

Wrap every 3 items and add a div - for each problem

I'm quite sure it's because your foreach() exists inside the if($counter % 3 == 2) statement and $name does not exist in $content_array, so the value of $name is always of iteration $counter % 3 == 2 and will echo as many times as the length of $content_array.

I must say, this loop construction is not very robust and readable. Consider rewriting your loop in a more "best practice" and scalable way. I don't know what your user/cms functions do on the background, so it's hard to point you to another approach. Also, try to properly escape everything you echo on the page. But that's all off topic.

So to the point, for a quick fix, try to include $name in $content_array like:

$content_array[ 'item-' . $counter ] = array( $expandedInfo, $name );

Then change your foreach() to this:

foreach ( $content_array as $item_id => $info_and_name ) {
echo '<div class="expanded" id="' . $item_id . '">';
echo $info_and_name[0];
echo $info_and_name[1];
echo '</div>';
}

I think that should do it ;)

Output foreach-item in div, but output every 2nd and 3rd item in wrapping div

You can use simple if-elseif-else construct to print the divs.

  • If the index of the current element % 3 is 0, it is a big div, else it is a small div.

  • If the index of the current element % 3 is greater than 0, it is a small div.

    • If it is 1, prepend an opening div tag.
    • If it is 2, append a closing div tag.

Snippet:

<?php

foreach ($array as $idx => $set) {
$mod = $idx % 3;
if($mod === 0){
echo "<div class='big'>$set</div>";
}elseif($mod === 1){
echo "<div><div class='small'>$set</div>";
}else{// if $mod is 2
echo "<div class='small'>$set</div></div>";
}
}

Online Demo

Wrap a div around every four divs

try this

<?php 
$i=0;
$wrap_count = 4; // you can change this no of divs to wrap
foreach ($categories as $category)
{
$i+=1;
if($i%$wrap_count==1)
{
echo '<div class="row">';
}
?>
<div class="col-lg-3 col-md-3">.....</div>
<?php
if($i%$wrap_count==0)
{
echo '</div>';
}
}

if($i%$wrap_count!=0)
{
echo '</div>';
}
?>

PHP foreach with counter wrap every 3 items inside a div

Please change this

 if ($counter % 3){

To

 if ($counter % 3 == 0) {

The reason is ($counter % 3) always returns a number either 0 or some integer. It would be always true for if and will execute, so you must check if return is 0. So your full code will be


$counter = 0;

foreach ($communities as $row => $value) {
if ($counter % 3 == 0) {
echo "<div class='community-images'>";
}
echo '<div class="col-md-3 animated" data-animation="' . ($counter % 2 ? 'fadeInUp' : 'fadeInDown') . '" data-animation-delay="300">';
echo '<a href="our-communities.php?newcommunity=' . $value['id'] . '"><img src="../images/communities/' . str_replace(" ", "-", strtolower($value['name'])) . '/' . $value['logo'] . '" width="150" /></a>';
echo '<h4>' . $value['name'] . '</h4>';

echo '<span><strong>Location: </strong>' . $value['locationLabel'] . '</span>';
echo '<span><strong>Starting At: </strong>$' . number_format($value['min_home_price']) . '</span>';

echo '</div>';
if ($counter % 3 == 0) {
echo '</div>';
}
$counter++;
}
?>

PHP: wrapping each group of 3 elements

In other words, you need to write div before each group of three items and end-div after each group of three items:

// $counter always tells the number of processed items
$counter = 0;

for ($i = 1; $i <= 10; $i++) {
// before a group of three, $counter is a multiple of three
if ($counter % 3 == 0) {
echo 'div <br />';
}

// process the item then count it
echo 'do stuff <br />';
$counter ++;

// after a group of three, $counter is a multiple of three
if ($counter % 3 == 0) {
echo 'end-div <br />';
}
}

// close the last group if it is not complete
if ($counter % 3 != 0) {
echo 'end-div <br />';
}

Check it online.



Related Topics



Leave a reply



Submit