inspireddesign Posted February 21, 2010 Share Posted February 21, 2010 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 More sharing options...
premiso Posted February 21, 2010 Share Posted February 21, 2010 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 More sharing options...
inspireddesign Posted February 21, 2010 Author Share Posted February 21, 2010 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 More sharing options...
khr2003 Posted February 21, 2010 Share Posted February 21, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.