justlukeyou Posted March 8, 2011 Share Posted March 8, 2011 Hi, I am trying to display products which return after 3. So there are 5 rows of 3. I have tried adding a loop but I just cant get it to work. Can anyone advise me on how I can insert a </br> after every 3 items. <?php $query = "SELECT * FROM productfeed LIMIT 0, 15"; $fetchdata = mysql_query($query) or die("query: $query<br>This has an error: " . mysql_error() . '<br>'); while($row = mysql_fetch_array($fetchdata)) { $id = $row['id']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; $i = $i; for($i=0; $i<=3; $i=$i+1) echo "<div class='productpagemenu'> <div class='productpagedescription'> <center> <a href='$link' target='_blank' >$description</a> </center> </div> <div class='productpageimage'> <center> <a href='$link' target='_blank'><img src='$image' width=\"95%\"/></a> </center> </div> <div class='productpageprice'> <center> <center>£ $price</center> </center> </div> <div class='productpagepricebutton'> <center> <center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center> </center> $i </br> </div> </div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230024-after-3/ Share on other sites More sharing options...
Fergal Andrews Posted March 8, 2011 Share Posted March 8, 2011 Hi justlukeyou, If I understand you correctly, this is what you want: <?php $query = "SELECT * FROM productfeed LIMIT 0, 15"; $fetchdata = mysql_query($query) or die("query: $query<br>This has an error: " . mysql_error() . '<br>'); $i = 1; while($row = mysql_fetch_array($fetchdata)) { $id = $row['id']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; echo "<div class='productpagemenu'> <div class='productpagedescription'> <center> <a href='$link' target='_blank' >$description</a> </center> </div> <div class='productpageimage'> <center> <a href='$link' target='_blank'><img src='$image' width=\"95%\"/></a> </center> </div> <div class='productpageprice'> <center> <center>&#163; $price</center> </center> </div> <div class='productpagepricebutton'> <center> <center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center> </center> </div> </div>"; if ($i % 3 == 0) { echo "<br />"; } $i++; } ?> cheers, Fergal Quote Link to comment https://forums.phpfreaks.com/topic/230024-after-3/#findComment-1184709 Share on other sites More sharing options...
Pikachu2000 Posted March 8, 2011 Share Posted March 8, 2011 Use the modulus operator to check if the number of iterations is divisible by 3, and if so echo the <br>. $i = 1: while(whatever) { if( $i % 3 === 0 ) { echo '<br>'; } $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/230024-after-3/#findComment-1184712 Share on other sites More sharing options...
taquitosensei Posted March 8, 2011 Share Posted March 8, 2011 try this. increment a variable then check the remainder e.g. $a=3; $a%3==0; echo ($a%3==0)?"<br>":""; $a=0; while($row = mysql_fetch_array($fetchdata)) { $id = $row['id']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; echo (($a%3)==0)?"<br>":""; echo "<div class='productpagemenu'> <div class='productpagedescription'> <center> <a href='$link' target='_blank' >$description</a> </center> </div> <div class='productpageimage'> <center> <a href='$link' target='_blank'><img src='$image' width=\"95%\"/></a> </center> </div> <div class='productpageprice'> <center> <center>&#163; $price</center> </center> </div> <div class='productpagepricebutton'> <center> <center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center> </center> $i </br> </div> </div>"; $a++; } Quote Link to comment https://forums.phpfreaks.com/topic/230024-after-3/#findComment-1184714 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.