【问题标题】:Wrap a div around every four divs每四个 div 环绕一个 div
【发布时间】:2014-08-02 10:14:03
【问题描述】:

我需要一些关于 OpenCart 的 php 循环的帮助。

我需要做的是每 4 个循环围绕数据输出包装一个 div。

我有以下

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

我明白了

<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>

但我希望得到的是

<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>

任何帮助将不胜感激:)

【问题讨论】:

  • 提示:对%(模数)运算符进行研究。

标签: php opencart


【解决方案1】:

试试这个

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

【讨论】:

  • 它正在工作。但是,在这里,现在有 10 个类别。所以我现在得到了这个&lt;div class"row"&gt;1,2,3,4&lt;/div&gt;&lt;div class"row"&gt;5,6,7,8,9,10&lt;/div&gt; 但是,我想要这个&lt;div class"row"&gt;1,2,3,4&lt;/div&gt;&lt;div class"row"&gt;5,6,7,8&lt;/div&gt;&lt;div class"row"&gt;9,10&lt;/div&gt;
  • 根据您的代码,最后一个 div 中包含 6 个类别。但是,我想在每个 div 中扭曲 4 个类别。所以,我认为,在这里,最后一个 div 中应该包含 2 个类别。
  • 再次检查是否经过测试,如果您有 10 个类别,则最后一个 div 将仅包含 2 个,请参阅eval.in/174828
  • 我又检查了一遍。但是,它在我的代码中不起作用。最后一个 div 包含 6 个类别。
【解决方案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,如果$categories是一个无键数组,即array('a', 'b', 'c'),那么你可以去掉i = 0;i++;,把foreach改成foreach ($categories as $i =&gt; $category) {


正如 zerkms 在评论中提到的那样,这里的魔力来自 % (Modulo operation),它本质上就像一个时钟 - 数字将从 0 向上递增,直到达到 12,然后再次重置为 0。所以时钟被称为模十二。在本例中,我们使用模四循环打印我们的开始和结束行元素。

模四将是序列0, 1, 2, 3, 0, 1, 2, 3, 0, 1, etc

【讨论】:

    【解决方案3】:

    试试这个:-

    <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】:

      您可以使用计数器变量。最初将其设置为等于 0。然后为每个循环检查其值。如果等于 0,则输出 &lt;div class="row"&gt; 并将变量值加 1。如果其值 > 0 且 <div class="col-lg-3 col-md-3">.....</div> 并将其值加 1。如果等于 5 则输出&lt;/div&gt; 并将计数器变量重置为 0。

      类似(代码测试):

      <?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】:

        Modulo 运算符在与您想要的一行中的项目数进行比较时将等于零。所以你可以在那里输出你的起始&lt;div&gt;

        要添加关闭 div,您需要检查 4 的模数是否等于 3,或者我们是否已达到项目总数,以防止缺少关闭 &lt;/div&gt;

        <?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】:

          我有一个替代选项,可能会根据您的阵列起作用。

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

          【讨论】:

            【解决方案7】:

            好吧,将它添加到您的代码中:

            //before foreach
            $i = 0;
            

            // 关闭前的 foreach 内部 }

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

            然后在添加任何 div 之前(可能在 foreach 的开头:

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

            在 $i++ 语句之前,添加:

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

            【讨论】:

              【解决方案8】:
              <?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;
              ?>
              

              适合我

              【讨论】:

                【解决方案9】:

                试试这个。它对我有用。

                <?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 ?>
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2012-02-15
                  • 2023-03-31
                  • 2014-11-26
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-04-02
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多