mysql - echo PHP array with while loop -


i'm using while loop echo php array :

$connect = mysqli_connect("localhost", "user", "password", "db"); $sql = "select * table order rand() limit 0,4"; $result = mysqli_query($connect, $sql);  if(mysqli_num_rows($result) > 0) {  echo'<section>         <div class="container">            <div class="row">              <h2 class="bold">title</h2>                <hr> ';                  while($row = mysqli_fetch_array($result))                 {                  echo'                 <div class="col-sm-3 col-md-3 col-md-push-3">                     <div class="portfolio-wrapper">                            <div class="portfolio-single">                             <div class="portfolio-thumb">                                 <a href="'.$row['link']." target="_blank">                                                        <img src="'.$row['image'].'" class="img-responsive" alt="'.$row['alt']."></a>                             </div>                         </div>                         <div class="portfolio-info" dir="rtl">                             <a href="'.$row['link2']." target="_self"><h2>                     </h2>                                 <h6>'.$row['title'].</h6>                             </a>                         </div>                     </div>                 </div>';              }                 echo'                 </div>                     </div>                     </section';             }                 ?> 

this code works fine me problem need echo 4 different

for example :

   first be: <div class="col-sm-3 col-md-3 col-md-push-9">    second: <div class="col-sm-3 col-md-3 col-md-push-3">    third: <div class="col-sm-3 col-md-3 col-md-pull-3">    last: <div class="col-sm-3 col-md-3 col-md-pull-9"> 

any ideas how can accomplish structure using while loop ?

use counter, , put classes array this:

$classes = [     "col-sm-3 col-md-3 col-md-push-9",     "col-sm-3 col-md-3 col-md-push-3",     "col-sm-3 col-md-3 col-md-pull-3",     "col-sm-3 col-md-3 col-md-pull-9" ];  $i = 0; while($row = mysqli_fetch_array($result)) {     if ($i > 3) {         $i = 0;     }     ?>     <div class="<?php echo $classes[$i]; ?>">         content comes here     </div>     <?php     $i++; } 

note

this resets counter, if there no index in array. script assumes, 4 rows.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -