Jump to content

Looping Problem


inspireddesign

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

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

Archived

This topic is now archived and is closed to further replies.

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