Wrap a div around every four divs

亡梦爱人 提交于 2020-01-01 09:51:11

问题


I need a little help with a php loop for OpenCart.

I need to do is wrap a div around the output of the data every 4 loops.

I have the following

<?php foreach ($categories as $category) { ?>
<div class="col-lg-3 col-md-3">.....</div>
<?php } ?>

I get this

<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>

But what I'm hoping to get is

<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>    
</div>

Any help would be greatly appreciated :)


回答1:


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>';
}
?>



回答2:


<?php

$i = 0;
foreach ($categories as $category) {

  if (($i % 4) === 0) {
    echo "<div class='row'>";
  }

  echo '<div class="col-lg-3 col-md-3"></div>';

  if (($i % 4) === 3) {
    echo "</div>";
  }

  $i++;
}

?>

Ps, If $categories is a non keyed array, ie array('a', 'b', 'c') then you can get rid of the i = 0; and i++; and change the foreach to foreach ($categories as $i => $category) {


And as zerkms mentioned in a comment, the magic here is coming from % (Modulo operation), which is essentially like a clock- the numbers will increment from 0 upwards until they hit 12, at which point it is reset to 0 again. So the clock is said to be modulo twelve. In this instance we're using modulo four to cyclically print our our opening and closing row elements.

Modulo four would be the sequence 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, etc




回答3:


Try this :-

<div class="row">
    <?php
    $i = 1;
   foreach ($categories as $category) { ?>

      <div class="col-lg-3 col-md-3">.....</div>

    <?php if ($i % 4 == 0){ ?>
    </div><div class="row">
    <?php } ?>
    <?php $i++; ?>
    <?php } ?>



回答4:


You may use a counter variable. Initially set it to equal 0. Then for each loop check its value. If it is equal to 0 then output <div class="row"> and add 1 to the variable's value. If its value is > 0 and < 5 then output <div class="col-lg-3 col-md-3">.....</div> and add 1 to it's value. If it is equal to 5 output </div> and reset the counter variable to 0.

Something like (code tested):

<?php
    $counter = 0;
    foreach ($categories as $category) {
                If ($counter == 0) {
                    echo '<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
';
    $counter++;
                } elseif ($counter > 0 && $counter < 3) {
                    echo '<div class="col-lg-3 col-md-3">.....</div>
';
    $counter++;
                } elseif ($counter == 3) {
                    echo '<div class="col-lg-3 col-md-3">.....</div>
</div>
';
                    $counter = 0;
                }
    }
    if ($counter > 0) {
                echo '</div>
';
    }
?>



回答5:


The Modulo operator will be equal to zero when used in a comparison with the number of items you would like in a row. So you can output your starting <div> there.

To add a closing div you will need to check the modulo of 4 is equal to 3 or if we have reached the total number of items, to prevent a missing close </div>.

<?php 

    // set a counter
    $rowCount = 0;

    // store the total number of categories minus 1 as we will be
    // counting from 0 with the counter

    $categoryTotal = count($categories) - 1;

    foreach($categories as $c){

        if($rowCount % 4 == 0){
            echo '<div class="row">';
        }?>

        <div class="col-lg-3 col-md-3""></div>

        <?php 

        // add your closing div if it is the end of a row or if there are no more items    

        if($rowCount % 4 == 3 || $rowCount == $categoryTotal){
            echo '</div>';
        }

        // increment your counter
        $rowCount++;

    } ?>



回答6:


Well, add this to your code:

//before foreach
$i = 0;

// inside the foreach before its closing }

$i++;
}//this closes the foreach

And then before adding any div (probably in the beginning of the foreach:

if($i % 4 == 0)
{
 echo "<div class='each-four-iterations'>";
}

And before the $i++ statement, add this :

if($i % 4 == 0)
{
 echo "</div>";
}



回答7:


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

Works Fine For me




回答8:


I have an alternative option that might work depending on your array.

$new_array = array_chunk($your_array, 4);

foreach($new_array as $group_of_$four){
  echo '<div class="row">';
  foreach($group_of_four as $one){
     // Single Item
  }
  echo '</div>';
}



回答9:


Try this one. It works for me.

<?php $count = 1; ?>
                <?php foreach ($model->userProfile->portfolio as $item): ?>
                    <?php if ($count % 4 == 1): ?>
                        <div class="row" style="margin-bottom: 30px;">
                    <?php endif ?>

                        <div class="col-md-3">

                        </div>

                    <?php if ($count % 4 == 0): ?>

                        </div>

                    <?php endif ?>

                    <?php  $count++ ?>
                <?php endforeach ?>


来源:https://stackoverflow.com/questions/25093916/wrap-a-div-around-every-four-divs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!