从 php 数组循环显示轮播每页 6 项

Loop from php array dispaly carousel 6 items per page

我有一个关联数组或对象

示例:

我怎样才能遍历数组来显示轮播以在这样的html输出

中输出
<div id="Carousel" class="carousel slide">
                <!-- Carousel items -->
                    <div class="carousel-inner">                    
                        <div class="item active">
                            <div class="row">
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-0"><img src="images/img1.jpg" alt="Image" style="max-width:100%;"></a></div>
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-1"><img src="images/img2.jpg" alt="Image" style="max-width:100%;"></a></div>
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-2"><img src="images/img3.jpg" alt="Image" style="max-width:100%;"></a></div>
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-3"><img src="images/img4.jpg" alt="Image" style="max-width:100%;"></a></div>
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-4"><img src="images/img5.jpg" alt="Image" style="max-width:100%;"></a></div>
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-5"><img src="images/img6.jpg" alt="Image" style="max-width:100%;"></a></div>
                            </div><!--.row-->
                        </div><!--.item-->              
                        <div class="item">
                            <div class="row">
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-0"><img src="images/img1.jpg" alt="Image" style="max-width:100%;"></a></div>
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-1"><img src="images/img2.jpg" alt="Image" style="max-width:100%;"></a></div>
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-2"><img src="images/img3.jpg" alt="Image" style="max-width:100%;"></a></div>
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-3"><img src="images/img4.jpg" alt="Image" style="max-width:100%;"></a></div>
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-4"><img src="images/img5.jpg" alt="Image" style="max-width:100%;"></a></div>
                              <div class="col-md-2"><a href="#" class="thumbnail" id="carousel-selector-5"><img src="images/img6.jpg" alt="Image" style="max-width:100%;"></a></div>
                            </div><!--.row-->
                        </div><!--.item-->
                    </div><!--.carousel-inner-->
                  <a data-slide="prev" href="#Carousel" class="left carousel-control">‹</a>
                  <a data-slide="next" href="#Carousel" class="right carousel-control">›</a>
                </div><!--.Carousel-->

我试过:

<div class="carousel-inner">     

                   <?php 
                      echo "<div class='item active'>";
                      echo "<div class='row'>";
                        foreach($arr as $key=>$value){
                            if ($key== 6) {
                                echo "</div>";
                                echo "</div>";
                                echo "<div class='item'>";
                                echo "<div class='row'>";
                            }
                             echo "<div class='col-md-2'>";
                             echo '<a href="#" class="thumbnail" id="carousel-selector-'.$key.'"><img src="images/img'.$value.'.jpg" alt="Image" style="max-width:100%;"></a>';
                            echo "</div>";
                        }
                    echo "</div>";  
                    echo "</div>";?>    

您的循环代码非常接近;您所需要的只是一个模运算符。

如果你不知道模是什么,你可以在这里阅读:http://php.net/manual/en/internals2.opcodes.mod.php

<?php 
  echo "<div class='item active'>";
  echo "<div class='row'>";
    foreach($arr as $key=>$value)
    {
        //if we can divide $key by six without remainder
        if ($key % 6 == 0) 
        {
            echo "</div>";
            echo "</div>";
            echo "<div class='item'>";
            echo "<div class='row'>";
        }
         echo "<div class='col-md-2'>";
         echo '<a href="#" class="thumbnail" id="carousel-selector-'.$key.'"><img src="images/img'.$value.'.jpg" alt="Image" style="max-width:100%;"></a>';
        echo "</div>";
    }
echo "</div>";  
echo "</div>";?>   

这是我的正确答案版本:

<?php
$result .= '<div class="item active">';
    foreach ($get_result as $key => $value) {
        if ($key !=0 && ($key % 6 == 0)) { //to avoid first empty "active"
          $result .= "</div>";
          $result .= "<div class='item'>";
        }

        $result .= '<a href="'.$value['url'].'"><img src="'.$value['logo'].'" alt="'.$value['name'].'" title="'.$value['name'].'"></a>';
    }
    $result .= '</div>';
    echo $result;
?>