Jump to content

Recommended Posts

Hello Everyone,

 

I have a looping problem that I can't seem to figure out.  In the code below I output a table with several rows and columns.  The output is based on the number of pilots that are being passed to the array.  So I know how many pilots there are and how to print the table with the correct number of columns and rows BUT... I want to loop through and start a NEW table when the number of pilots reaches 4.  So if there are 12 pilots there would be 3 tables with 5 columns having the correct data from the queries array. 

 

I believe I'm on the right track but can't seem to get the logic figured out. Can someone help me out?  Thanks.

 


						foreach ( $listPilots as $Pilot ) {

							for ( $colCount = 1; $colCount < 2; $colCount++ ) {

								$pilotCount .= '<td width="153"><p align="center"><strong> PILOT '.$pCount++.'</strong></p></td>';
								$pFirst 	.= '<td width="153"><p>'.$Pilot->pilot_fname.'</p></td>';
								$pLast 		.= '<td width="153"><p>'.$Pilot->pilot_lname.'</p></td>';
								$pAge 		.= '<td width="153"><p>'.$Pilot->pilot_dob.'</p></td>';
								$pMed 		.= '<td width="153"><p>'.$Pilot->pilot_cert_med.'</p></td>';

							}


						}


							$pilots = '<table border="0" cellspacing="0" cellpadding="0" style="font-family:Arial, Helvetica, sans-serif; font-size:10px;">
										  <tr>
											<td width="153"><p><strong><em><u>PILOTS:</u></em></strong></p></td>
											'.$pilotCount.'
										  </tr>
										  <tr>
											<td width="153"><p><strong>FIRST NAME.................</strong></p></td>
											'.$pFirst.'
										  </tr>
										    <td width="153"><p><strong>LAST NAME............</strong></p></td>
											'.$pLast.'
										  </tr>
										  <tr>
											<td width="153"><p><strong>AGE..................</strong></p></td>
											'.$pAge.'
										  </tr>
										  <tr>
											<td width="153"><p><strong>MEDICAL DATE..........</strong></p></td>
											'.$pMed.'
										  </tr>

										</table>';

					die($pilots);

Link to comment
https://forums.phpfreaks.com/topic/192770-looping-problem/
Share on other sites

You would use the modulus operator (%) in an if statement:

 

if (($i%4) == 0) {
//start new table
}

 

Basically if $i / 4 has a remainder then it will skip over it, but if the remainder = 0 it will enter the if meaning that $i is divisible by 4.

Link to comment
https://forums.phpfreaks.com/topic/192770-looping-problem/#findComment-1015467
Share on other sites

I don't believe that your solution will do what I want or I just don't understand it. The count of pilots or $i (in that case you provided) will always have a value.  Is there a way to loop through so for the first 4 it prints the table of 4 pilots and then for the seconded four it prints the table of the next four pilots and so on?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/192770-looping-problem/#findComment-1015476
Share on other sites

I think  premiso solution should work fine:

look at this code:

$i = '0';
echo '<table>';
foreach ( $listPilots as $Pilot ) {
echo '<tr><td>$pilot</td></tr>';
// if number of pilots is divisable by four (or any other number you choose) without a reminder then close the table and start a new one
if (($i%4) == 0) {
// start new table
echo '</table><table>
}
echo '</table>

Link to comment
https://forums.phpfreaks.com/topic/192770-looping-problem/#findComment-1015529
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.