Reputation: 1681
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'] ." ".
$row['model'] ."<br>".
$row['color'] ."<br>".
$row['storage'] ."<br>" ,"<br>".
number_format($row['price']) ." " ."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
Upvotes: 0
Views: 234
Reputation: 103
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'] ." ".
$row['model'] ."<br>".
$row['color'] ."<br>".
$row['storage'] ."<br>" ,"<br>".
number_format($row['price']) ." " ."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>';
Upvotes: 0
Reputation: 449
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.
Upvotes: 1