【问题标题】:Display multiple images from database to bootstrap carousel显示数据库中的多个图像以引导轮播
【发布时间】:2015-05-12 21:04:08
【问题描述】:

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

[问题] 我是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 图像到轮播,但不知道该走哪条路。

【问题讨论】:

    标签: php html css twitter-bootstrap carousel


    【解决方案1】:

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

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

    如果您在实现代码时遇到问题,或者您不明白我在这里做了什么,请不要犹豫。

    【讨论】:

    • 对不起,但我在实现代码时遇到了问题,现在真的不知道在哪里放什么。感谢您的帮助
    • 用我的代码代替你的代码,以 '
      ' 开头并以 '
      ' 结尾并替换 my '... 输出图像的代码 ...' 与
    • 它部分工作,当我刷新页面时,第一个显示所有图片,然后是 4 x 4,但一直显示所有图像。
    • 你能把你的整个代码粘贴到 JSFiddle,这样我就可以看到你做了什么。
    猜你喜欢
    • 2016-11-19
    • 2020-10-17
    • 1970-01-01
    • 2018-01-12
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多