Jump to content

[SOLVED] interrupt while loop, then resume?


theredking

Recommended Posts

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.