ibinod Posted July 5, 2008 Share Posted July 5, 2008 Hi, wt i am trying to do is breaking a while loop, it's executed with a mysql query my mysql database consist of a table called pics and has a colum id which consist around 20 feilds and i need to execute in this way so the output becomes like this <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> till now i did it in this way <table width="860px" align="center" cellpadding="0" cellspacing="0" border="0" class="randimg"> <tr> <?php $sql = mysql_query("SELECT * FROM pics ORDER BY id DESC"); while($pics = mysql_fetch_array($sql)) { $id = $pics['id']; $cat = $pics['cat']; $pic = $pics['pic']; echo "<td></td>"; } ?> </tr> </table> it gives me the output like this <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> pls help me i out to break this while loop after every 5 ids from the pics table and 5 <td> </td> tags are executed and again i want to continue it from there after inserting <tr> tag Quote Link to comment https://forums.phpfreaks.com/topic/113330-pls-help-me-with-while-statement/ Share on other sites More sharing options...
papaface Posted July 5, 2008 Share Posted July 5, 2008 So what do you want? We're not mind readers. Quote Link to comment https://forums.phpfreaks.com/topic/113330-pls-help-me-with-while-statement/#findComment-582271 Share on other sites More sharing options...
ibinod Posted July 5, 2008 Author Share Posted July 5, 2008 i want it to execute first 5 queries like this <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> and again continue it the same way from the 6th query <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> i trired a lot i only get this <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> so i came over here searching for help and solution Quote Link to comment https://forums.phpfreaks.com/topic/113330-pls-help-me-with-while-statement/#findComment-582273 Share on other sites More sharing options...
papaface Posted July 5, 2008 Share Posted July 5, 2008 Do you want: <table width="860px" align="center" cellpadding="0" cellspacing="0" border="0" class="randimg"> <?php $sql = mysql_query("SELECT * FROM pics ORDER BY id DESC"); $i = 0; while($pics = mysql_fetch_array($sql)) { echo "<tr>"; $id = $pics['id']; $cat = $pics['cat']; $pic = $pics['pic']; echo "<td></td> </tr>"; } ?> </table> You're not explaining yourself very well. Quote Link to comment https://forums.phpfreaks.com/topic/113330-pls-help-me-with-while-statement/#findComment-582278 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2008 Share Posted July 5, 2008 You need to count how many items your outputing. Something like this: <table width="860px" align="center" cellpadding="0" cellspacing="0" border="0" class="randimg"> <?php $sql = mysql_query("SELECT * FROM pics ORDER BY id DESC"); $tmp = array(); $tbl = array(); while($pics = mysql_fetch_assoc($sql)) { $tmp[] = $pics['id'] . ' ' . $pics['cat'] . . $pics['pic']; if (count($tmp) == 5) { $tbl[] = '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>'; $tmp = array(); } } if (!empty($tmp)) $tbl[] = '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>'; implode("\n",$tbl); ?> </table> Note: not tested. Ken Quote Link to comment https://forums.phpfreaks.com/topic/113330-pls-help-me-with-while-statement/#findComment-582279 Share on other sites More sharing options...
ibinod Posted July 5, 2008 Author Share Posted July 5, 2008 Hello kenrbnsn, thanx a lot for ur reply but i couldn't get ut wirj actually i want the query to execute all the rows from my mysql table, i don't want to limit it, currently there are 20 rows but they will increase once more stuffs are added so i don't want to limit it with a specified value all i want to is insert <tr> </tr> tag after each 5 <td></td> tags. Quote Link to comment https://forums.phpfreaks.com/topic/113330-pls-help-me-with-while-statement/#findComment-582307 Share on other sites More sharing options...
wildteen88 Posted July 5, 2008 Share Posted July 5, 2008 kens code does exactly that. It doesn't limit any thing. Quote Link to comment https://forums.phpfreaks.com/topic/113330-pls-help-me-with-while-statement/#findComment-582326 Share on other sites More sharing options...
ibinod Posted July 5, 2008 Author Share Posted July 5, 2008 thanx a lot wildteen, i again tried making up and i see it really works as i wanted, thank you very much ken for the help thanx this is really wonderful, and it's a great community came here today and got my solution within minutes, it's just wonderful thank you very much again, Quote Link to comment https://forums.phpfreaks.com/topic/113330-pls-help-me-with-while-statement/#findComment-582335 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.