0

极客请帮忙,

我在 while 循环中使用数组键时遇到问题。

我正在尝试通过使用以下代码从数据库循环来显示 twitter 引导滑块图像:

<div id="slider">
         <!--Code for make home images slide show-->
                <div id="myCarousel" class="carousel slide">

              <!-- here comes the engine to run the sliders -->
              <?php  

              $query = 'SELECT * FROM banners WHERE status = 1 ORDER BY id ASC';

             ?>

                     <div class="carousel-inner">
                      <?php
                      if ($r = mysql_query($query , $conn)) {  //run the query
                      $row = mysql_fetch_array($r);
                      reset($row);

               while (list($key, $value) = each($row)) {
                   if ($key == 0) {   ?>
                        <div class="item active">
                    <img src="images/homeimages/<?php echo $value['image'];?>" alt="<?php echo $value['title'];?>">
                    <!-- <img src="images/homeimages/one.jpg" alt=""> -->
                    <div class="carousel-caption">
                      <!-- <h4>First Thumbnail label</h4> -->
                      <p><i><?php echo $value['title'];?></i></p>
                    </div>
                  </div>
                  <?php }else{ ?>

                       <div class="item">
                    <img src="images/homeimages/<?php echo $value['image'];?>" alt="<?php echo $value['title'];?>">
                    <!-- <img src="images/homeimages/one.jpg" alt=""> -->
                    <div class="carousel-caption">
                      <!-- <h4>First Thumbnail label</h4> -->
                      <p><i><?php echo $value['title'];?></i></p>
                    </div>
                  </div>

                  <?php }

                 }
               }else {
                print '<p style="color: red;">Could not retrieve the slider:<br />' . mysql_error($dbc) . '.</p>';
              } ?>


                </div>  


              <!--   <a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>
                <a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a> -->
                <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>


    </div><!-- endof slider  -->`

. 众所周知,twitter bootstrap 的第一个滑块图像必须具有 active 类,以确保它加载得更快。我需要从数组中选择第一个滑块图像并将活动类添加到其中,而其余的滑块图像在没有活动类的情况下保持不变。

有人可以告诉我我在哪里弄乱了我的代码吗?

4

1 回答 1

1

尝试这个:

<div class="carousel-inner">
<?php
if ($r = mysql_query($query , $conn)) {
    $count = 0;
    while ($row = mysql_fetch_array($r){
        if ($count == 0){?>
            <div class="item active">
        <?php}
        else { ?>
            <div class="item">
        <?php}?>
        <img src="images/homeimages/<?php echo $row['image'];?>" alt="<?php echo $row['title'];?>">
         <div class="carousel-caption">
             <p><i><?php echo $row['title'];?></i></p>
         </div>
         </div>    
        <?php
        }
        $count ++;
    }
}
else {
    print '<p style="color: red;">Could not retrieve the slider:<br />' . mysql_error($dbc) . '.</p>';
} 
于 2013-07-23T00:07:12.957 回答