简体   繁体   中英

Display 4 result per row whatever number of queries

echo '<table class="table-condensed"  style="width:700px; margin-left:25%;margin-top:-270px;" id="table1">';

$i = 0;

while($row = mysqli_fetch_array($query)) { 
    $image = $row['name']; 
    $product_id = $row['product_id'];
    $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $image);

    if($i == 0){
        echo '<tr>';
    }

    echo '<td style=text-align:center; font-size:12px;>'.'<img src="admin/image/'.$image.'" style="height:100px; width:auto;"/>'.'<br>';

    echo $row['brand'] ."&nbsp;". 
         $row['model'] ."<br>".
         $row['color'] ."<br>".
         $row['storage'] ."<br>" ,"<br>".
         number_format($row['price']) ."&nbsp;" ."Php". "<br>";

    echo '<a href="mobile.php?product_id=<?php echo $product_id?>"><input type="submit" value="View this item" class="btn-primary"></a>'.'</td>';   

    if($i > 4){ 
        $i = 0;
        echo '</tr>';
    };

    $i++;   

}   

echo '</table>';

I have 9 queries now on my database, and it shows

img1 img2 img3 img4 img5 img6
Img7 img8 img9   //which is wrong

I want it:

Img1 img2 img3 img4
img5 img6 img7 img8
img9 img10 img11 img12

That every time I will add on my query the table row will not be change to 4 image per row. Thankyou you in advance

First of all learn Math. You are starting to count at

$i = 0 

and in your condition is

if ($i > 4)

which is true with exactly 6th option. Your sequence is:

0 1 2 3 4 5

Also, you are incrementing your $i value after you reset it, so next loop will start with $i == 1 next

<tr> 

tag will not be printed, because it checks for zero.

Try this, should work :

  echo '<table class="table-condensed"  
  style="width:700px; margin-left:25%;margin-top:-270px;" id="table1">';

  $i = 0;


while($row = mysqli_fetch_array($query)) { 
   $image = $row['name']; 
   $product_id = $row['product_id'];
   $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $image);

if ($i % 4 == 0) { echo '<tr>'; }

echo '<td style=text-align:center; font-size:12px;>'.'<img src="admin/image/'.$image.'" style="height:100px; width:auto;"/>'.'<br>';

echo $row['brand'] ."&nbsp;". 
     $row['model'] ."<br>".
     $row['color'] ."<br>".
     $row['storage'] ."<br>" ,"<br>".
     number_format($row['price']) ."&nbsp;" ."Php". "<br>";

echo '<a href="mobile.php?product_id=<?php echo $product_id?>"><input type="submit" value="View this item" class="btn-primary"></a>'.'</td>';   

  if ($i % 4 == 2) { echo '</tr>'; }

   $i++;   

}   
    if ($i % 4 != 0) { echo '</tr>'; }
  echo '</table>';  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM