循环以从db逐个显示图像到bootstrap轮播

时间:2015-11-28 09:11:01

标签: php twitter-bootstrap codeigniter codeigniter-2 bootstrap-carousel

尝试在codeigniter中的foreach循环中显示来自数据库的轮播图像。以下代码一次显示所有图像。如何逐个在滑块内显示图像?下一个和上一个按钮也不起作用?

<div id="myCarousel" class="carousel slide carousel-fade" data-ride="carousel">

    <!-- Indicators -->
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>

    <div class="carousel-inner" role="listbox">

    <?php $i = 1; ?>
    <?php $item_class = ($i == 1) ? 'item active' : 'item'; ?>
    <div class="<?php echo $item_class; ?>">=
        <a href="#">
            <?php foreach ($m->result() as $row)  :
              $Player=$row->image;
              echo "<img src='http://opunletter.com/" .  $Player . "'";
            ?>
        </a>
    </div>
    <?php $i++; ?>
    <?php endforeach; ?> 

    </div>

    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>

    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>

</div><!-- /.carousel -->

2 个答案:

答案 0 :(得分:1)

<?php
$i  = 1;
foreach ($m->result() as $row) {
    $Player     = $row->image;
    $item_class = ($i == 1) ? 'item active' : 'item';
?>
<div class="<?php echo $item_class; ?>">
    <a href="#">
        <img src="http://opunletter.com/<?php echo $Player; ?>" />
    </a>
</div>
<?php
    $i++;
}
?> 

你需要将div.item保留在循环中以封闭每个单独的图像。

答案 1 :(得分:0)

 <!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">

  <div class="item active">
    <img class="img-rounded img-responsive mainpic" src='<%#"/ProfilePictures/" +Eval("ProfilePicture") %>' alt='<%#Eval("Username") %>'  onerror="this.src='/images/default-image.png'" style="width: 100%; height: 100%; max-width:512px;max-height:512px;">
    <div class="carousel-caption">
      <h3><%#Eval("Username") %></h3>
      <p>'<%#Eval("Mobile") %>'</p>
    </div>
  </div>


  **<asp:Repeater ID="Repeater3" runat="server" DataSourceID="SqlDataSource2"><ItemTemplate>**

  <div class="item">
    <img class="img-rounded img-responsive mainpic" id="po" runat="server" src='<%#Eval("Picture") %>' onerror="this.src='/images/default-image.png'" alt="" style="width: 100%; height: 100%;max-width:512px;max-height:512px;">
    <div class="carousel-caption">
        <h3><%#Eval("Productname") %></h3>
      <p>'<%#Eval("Productdiscription") %>'</p>
    </div>
  </div>


      **</ItemTemplate></asp:Repeater>**   

我只是为asp.net应用程序做了这个,我没有在其他地方改变任何东西,并且它有效。因为我不需要,我没有在有序列表上投入精力。并不像看起来那么复杂。