theredking Posted December 26, 2008 Share Posted December 26, 2008 Hello all, just a quick question: I have a while loop populating a table for which I've already written a header. This table ends up having quite a number of rows, and the information in the header is lost as you scroll down the table. Is there a way to interrupt the while loop, re-insert the header information and then continue the loop until it finishes? Thanks all in anticipation.. Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/ Share on other sites More sharing options...
carrotcake1029 Posted December 26, 2008 Share Posted December 26, 2008 How does it get lost? Could you show some code of what you are doing so we can get a better understanding? Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-724053 Share on other sites More sharing options...
9three Posted December 26, 2008 Share Posted December 26, 2008 You can use break and continue Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-724096 Share on other sites More sharing options...
hobeau Posted December 26, 2008 Share Posted December 26, 2008 I definitely need to see your code but it sounds like what your trying to do is generate the following: <table> <tr> <th>header1</th> <th>header2</th> </tr> <tr> <td></td> <td></td> </tr> ... more rows and cells... <tr> <th></th> <th></th> </tr> ...so on and so forth... </table> If this is the case you would simply need to have a counting variable that starts at zero and increments each iteration of the loop ($i++). Then you would need to have an 'if' statement that would check the value of the counter and if the value is somewhere say 15, you would concatenate the table headers and reset the counter variable back to 0 ($i = 0;) and the loop would continue on adding headers every 15 rows. Of course you can change this to any number you wish, you would just need to specify this in the if statement. Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-724117 Share on other sites More sharing options...
theredking Posted January 8, 2009 Author Share Posted January 8, 2009 Wow, sorry all, I didn't get any notification that there had been replies to this topic. Thank you so much, I'll take a look at this and get right back to you all. Thank you again! Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-732505 Share on other sites More sharing options...
theredking Posted January 8, 2009 Author Share Posted January 8, 2009 I've cut out most of the code to just show you the loop. I'm not quite sure how to get the if statement worked into here and then reset the loop. Thanks again. $num = mysql_numrows($result); $i = 0; while ($i < $num) { print "data goes here."; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-732521 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 I've cut out most of the code to just show you the loop. I'm not quite sure how to get the if statement worked into here and then reset the loop. Thanks again. $num = mysql_numrows($result); $i = 0; while ($i < $num) { print "data goes here."; $i++; } $num = mysql_numrows($result); $i = 0; while ($i < $num) { if ($i = ceil($num/2)) { echo 'table header info here'; } print "data goes here."; $i++; } You do not really want to "break" or "continue", you just want to add another row halfway through the results, That should do it, and if /2 does not give it enough try /3 or /4 etc until you get it how you want. Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-732524 Share on other sites More sharing options...
theredking Posted January 8, 2009 Author Share Posted January 8, 2009 Hi Premiso, I just tried your suggestion, and that seems to insert the header information after every results row that is printed... Is that what it should do? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-732549 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 Sorry, syntax mistake: if ($i == ceil($num/2)) { Replace that line above, before it had a single = it should be == for comparison. Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-732552 Share on other sites More sharing options...
theredking Posted January 8, 2009 Author Share Posted January 8, 2009 That's it, thank you! Fantastic!! Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-732555 Share on other sites More sharing options...
theredking Posted January 8, 2009 Author Share Posted January 8, 2009 Ok, this is almost exactly what I want it to do now, but it only re-inserts this header information once. Is there a way to tell it to insert it every 10 or twelve rows? I have tried: if ($i == 10) { but obviously this only inserts it after the 10th row, it doesn't do it every 10 rows. I'm going to keep trying to figure this out, but any help would be greatly appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-732576 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 Ok, this is almost exactly what I want it to do now, but it only re-inserts this header information once. Is there a way to tell it to insert it every 10 or twelve rows? I have tried: if ($i == 10) { but obviously this only inserts it after the 10th row, it doesn't do it every 10 rows. I'm going to keep trying to figure this out, but any help would be greatly appreciated. Thanks if (($i%10) == 0) { The modulus operator is what you want. Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-732578 Share on other sites More sharing options...
theredking Posted January 8, 2009 Author Share Posted January 8, 2009 Perfect. Thank you so much for your time. Quote Link to comment https://forums.phpfreaks.com/topic/138482-solved-interrupt-while-loop-then-resume/#findComment-732583 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.