zhTonic Posted November 6, 2008 Share Posted November 6, 2008 Hey there, I'm not completely done with my code here but what i want to do is have it loop 5 results and then start a brand new row after 5 and then continue on with the loop. As an example if i have 12 rows in my table i would want the output to look something like this. 1 2 3 4 5 6 7 8 9 10 11 12 I'm new to using while loops for this purpose so i need some help thanks function is below.(ignore the LIMIT 5 i was just looking at how 5 results looked) function productsforsale($font){ $sql = mysql_query("SELECT * FROM products LIMIT 5"); echo " <TR> <TD class=maintext bgColor=#c2b868> <TABLE cellSpacing=0 cellPadding=0 width=740 border=0> <TBODY> <TR> <TD colSpan=6 height=10></TD></TR> <TR> <TD> </TD> <TD colSpan=5> <DIV align=center> <P><STRONG>$font Click on an image to link to each section below. </STRONG></P></DIV></TD></TR> "; echo "<TR> <TD></TD> <TD colSpan=5 height=15></TD></TR> <TR> <TD width=20> </TD>"; while($sqlly = mysql_fetch_array($sql)){ $title = $sqlly["title"]; $image = $sqlly["image"]; $prod = $sqlly["prod"]; echo "<TD vAlign=top align=middle width=144><A href='?action=productsforsale&prod=$prod'><IMG height=75 src='images/$image' width=100 border=0><BR>$font $title</A></TD>"; } echo "</TR><TR> <TD> </TD> <TD align=middle colSpan=5 height=15></TD></TR>"; } Link to comment https://forums.phpfreaks.com/topic/131567-solved-need-some-help-with-a-while-loop-thanks/ Share on other sites More sharing options...
trq Posted November 6, 2008 Share Posted November 6, 2008 See the example in the FAQ. Link to comment https://forums.phpfreaks.com/topic/131567-solved-need-some-help-with-a-while-loop-thanks/#findComment-683350 Share on other sites More sharing options...
anatak Posted November 6, 2008 Share Posted November 6, 2008 what you want is a while loop and a nested if loop $i = 0 //counter while($sqlly = mysql_fetch_array($sql)){ if($i=<5){ insert html to create a new row $i=0 }else{ $i++; } insert html to create a new cell in your row } btw HTML should not be written in CAPS <TR><TD></TD></TR> is wrong <tr><td></td></tr> is correct Link to comment https://forums.phpfreaks.com/topic/131567-solved-need-some-help-with-a-while-loop-thanks/#findComment-683357 Share on other sites More sharing options...
zhTonic Posted November 6, 2008 Author Share Posted November 6, 2008 Thank you both for the help! Link to comment https://forums.phpfreaks.com/topic/131567-solved-need-some-help-with-a-while-loop-thanks/#findComment-683377 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.