This is a very basic problem but I am very new to PHP.
I need to show results in such scenario that only three records in same line then add <br /> and then on next line, same thing should happend. I am unable to make its logic and in a great trouble 🙁
Right now, I am just using the simple way i.e.
while($data = mysql_fetch_array($res_set)) {
?>
<div><?php echo $data['name']?><img src="images/<?php echo $data['image']?>" /></div>
<?php
}
this is my code
$cnt = 0;
while ($prd = mysql_fetch_array($res)) {
?>
<div class="imageRow">
<?php
$cat_id = $prd['cat_id'];
$sql = "select * from tbl_category where id = $cat_id";
$cat_res = mysql_query($sql);
$cat_data = mysql_fetch_array($cat_res);
$cat_name = $cat_data['name'];
?>
<div class="set">
<div class="single first">
<a href="<?php echo $ru ?>admin/product_images/<?php echo $cat_name ?>/thumb/<?php echo $prd['thumb_img'] ?>" rel="lightbox[plants]"><img src="<?php echo $ru ?>admin/product_images/<?php echo $cat_name ?>/large/<?php echo $prd['thumb_img'] ?>" style="height: 100px; width: 100px;" /></a><br />
<a href="<?php echo $ru ?>contact_form.php?id=<?php echo $prd['id'] ?>">Choose</a>
</div>
</div>
</div>
<?php
$cnt = $cnt++;
if($cnt%3 == 0) {echo "<br />";}
}
//echo $cnt;
?>
any help will be appreciated.
Thanks
Explanation:
Set the variable $i to a number, which will then be used to keep track of how many items you’ve written.
Then, within the while loop, increment $i ($i++), which is the same as $i = $i + 1; By doing this, you always know which item you’re on – whatever the value of $i is. Some people choose to set it to 1 initially then increment at the very end – other like to set it to 0, and set it near the beginning – either is completely fine – whatever you need it to do / whichever way you like better.
Lastly, check if $i is evenly divisible by 3 (kind of like remainder – it’s called “mod” and is represented by the percent symbol). If it is, then echo a line break.