0

geeks please help,

I have a problem with the usage of array keys with the while loop.

I'm trying to display the twitter bootstrap slider images by looping from the database with this code:

<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  -->`

. as`it's well known the first slider image of twitter bootstrap must have the class of active to make sure it loads faster. I need to pick the first slider image from an array and add the class of active into it while the rest of the slider images remains the same without an active class.

can someone please tell me where i'm messing up with my codes?

3
  • why can't you use this code to iterate through the resultset: while ($row = mysql_fetch_array){ // do something here } you can still access all the keys/values that you need. Commented Jul 22, 2013 at 23:58
  • May you please write down how i'm going to use while ($row = mysql_fetch_array){ ? That's something i don't know @Maximus2012 Commented Jul 23, 2013 at 0:04
  • see my answer. Understand that mysql_ functions are not secure and deprecated but this should still serve as a solution for you. Later you can always change your code to make use of mysqli/PDO. Commented Jul 23, 2013 at 0:08

1 Answer 1

1

Try this:

<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>';
} 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.