简体   繁体   English

在foreach循环中的每个具有6个内容的div之后添加一个具有5个内容的div

[英]Adding a div with 5 contents after every div with 6 contents in a foreach loop

So basically what I need to output is this.. 所以基本上我需要输出的是这个

在此处输入图片说明 在此处输入图片说明

I currently have this code. 我目前有此代码。 I'm very confused on how to proceed on this one. 我对如何进行此操作感到非常困惑。

  <?php
    $terms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22];
  ?>

  <div class="container text-center">
    <?php $count = 0; ?>
    <div class="row">
      <?php
        foreach($terms as $term) : ?>
        <?php if ($count == 6) :  ?>
     </div> 
      <div class="row">
      <?php $count = 1; endif; ?>

      <div class="col-sm-2">
      <?php echo $term; ?>
      </div>
      <?php $count++; endforeach; ?>
    </div>  
  </div>

Haven't tried but should work out if you have that $table array... Hope it helps... 还没有尝试过,但是如果您有该$ table数组,应该可以解决...希望对您有所帮助...

$table = [ 
     0 => [1, 2, 3, 4, 5, 6],
     1 => [1, 2, 3, 4 ,5],
     2 => [1, 2, 3, 4, 5, 6],
     3 => [1, 2, 3, 4, 5]
]

 <div class="container text-center">
       <?php 
        foreach($table as $div) {
           echo '<div class="row">';
           foreach($div as $number) {
                if(count($div) == 5) {
                 $add_offset = " col-md-offset-1 ";
                }
                echo '<div class="col-md-2 '.$add_offset.'"';
                echo $number;
                echo '</div>';
           }
           echo '</div>';
        }
       ?>
 </div>

EDIT: Again, haven't tried it but you might have an idea of how it should work, maybe adapt a little bit if you get some errors or not exactly what you wanted. 编辑:同样,还没有尝试过,但是您可能对它应该如何工作有一个想法,如果遇到一些错误或者不是您想要的,可能会稍作调整。 Worth the shot. 值得一试。

<?php
$terms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22];   
$count = 0;
$ct = 0;

echo '<div class="container text-center">';
while($count != floor(count($terms)%6)) {
    echo '<div class="row">';
        $repeat = 6;
        if($count % 2 == 1) {
            $add_class = "col-md-offset-1";
            $repeat = 5;
        }
        while($ct != $repeat) {
            echo '<div class="col-sm-2 '.$add_class.' ">'.$ct.'</div>';
            $add_class = '';
            $ct++;
        }

    echo '</div>';
    $count++;
}
echo '</div>':

You can use CSS to select patterns of child elements. 您可以使用CSS选择子元素的模式。 In this example, every 7th & 11th item are given unique margin properties that interact with other child elements. 在此示例中,每个第7和第11个项目都具有与其他子元素进行交互的唯一边距属性。 No need for PHP loops. 无需PHP循环。

 .wrapper { max-width: 320px; text-align: center; } .item { display: inline-block; width: 48px; height: 46px; background-color: #000; } .item:nth-of-type(11n+0){ margin-right: 24px; } .item:nth-of-type(11n-4){ margin-left: 24px; } 
 <div class="wrapper"> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM