php - 显示从数据库到 Bootstrap 轮播的多个图像

标签 php html css twitter-bootstrap carousel

我在数据库中有很多图像,我想使用 php 在 Bootstrap 轮播中显示它们。

[问题] 我是 php 的菜鸟,所以我碰壁了。让我用代码来解释。

<div class="carousel-inner">
  <div class="item active">
    <div class="row">
      <div class="col-md-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></a></div>
      <div class="col-md-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></a></div>
      <div class="col-md-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></a></div>
      <div class="col-md-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></a></div>
    </div><!--.row-->
  </div><!--.item-->  

  <?php
  $pdo = connect();
  // display the list of all members in table view
  $sql = "SELECT * FROM filmovi";
  $query = $pdo->prepare($sql);
  $query->execute();
  $list = $query->fetchAll();
  ?> 

  <div class="item">
    <div class="row">
      <?php foreach($list as $rs) { ?>    
        <div class="col-md-3"><a href="#" class="thumbnail"><img src="assets/img/movies/<?php echo $rs['slika'] ?>" alt="Image" style="max-width:100%;"></a></div>
      <?php } ?>
    </div><!--.row-->
  </div><!--.item-->
</div>

如您所见,轮播一次显示 4 张图片,然后再显示 4 张,依此类推。在 foreach 循环中,就像现在一样,我一次获得所有图像,并且事件元素为空。

我需要从数据库中获取 4 x 4 图像到轮播,但不知道该走哪条路。

最佳答案

您需要指定一个 counter这将在每 4 次迭代后检查,以创建一个新的 <div class="item">...</div>元素。

<?php 
$counter = 0; //Set the counter to zero
  foreach ($list as $single_list){
    if($counter % 4 == 0) { // On every fourth iteration create a new item and row DIV
      echo '<div class="item"><div class="row">';
    }

      ... your code to output the images ...

    if($counter % 4 == 0) {
      echo '</div></div>'; // Close the row and item DIV
    }
$counter++;
}

如果您在执行代码时遇到问题,或者您不明白我在这里所做的事情,请随时提问。

关于php - 显示从数据库到 Bootstrap 轮播的多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30201365/

相关文章:

php - SQL/PHP 如何使用 LEFT JOIN 对其进行过滤?

php - 每 60 秒 AJAX Div 检索一次

jquery - 用于旧浏览器的 jquery 中的第 nth-child(4n)

javascript - 如何为angularjs应用程序提供响应能力

html - 在 Bootstrap 行中重复 html 的背景图像

php - 从跑道验证 webhook

php - 获取最大值的名称 max()

jquery - 从选定的下拉列表中添加 Bootstrap 标签类

html - 我需要为此创建额外的 CSS 类吗?

css - Bootstrap 对齐不同列大小的输入和标签