Jump to content

Recommended Posts

Hello all,

 

I would like to loop through a database and get a "chuck" of data then loop through again get another chunk of data until the total number is reached.  How would I handle this?  I think this can be handled with an array but don't know where to start.  So in the below code I have $totalPilots = 12.  The offset is at 0 (starting point).  I realizes that this could be considered as SQL but I'm not really having an issue with pulling data.  My problem is how to loop through and get a chunk of data and output the first chunk in an array so it can be displayed ($tableOutput) then go through and again and get another junk but now my $offset would would be starting at 3 not 0.  So I keep going until the $totalPilots is reached.

 

My goal is to have tables of 4 pilots in each table.  There might be a better way of handling this but not sure.  If you notice in the code I do a foreach loop, so if I dump all 12 pilots in the array I can't segment the pilots into chucks of 4 to use in the table output because it would just be one table of all 12 pilots.  At least I don't know how anyway.  I hope this made sense ;).

 

Thanks for any help on this one. 

 


		$totalPilots = 12;
		$offset = 0;
		$loopTimes = floor($totalPilots / 4); // Number of times to loop through the data
                        

			$sql = 'SELECT * FROM pilots WHERE pilot_aircraft=' . $Aircraft->aircraft_id . ' LIMIT '.$offset.',4' ;
			$listPilots = pilotData::find_by_sql($sql);

			foreach ( $listPilots as $Pilot ) {

					$pFirst 	.= '<td width="153"><p>'.$Pilot->pilot_fname.'</p></td>';

			}

			$TableOutput .= '<table border="0" cellspacing="0" cellpadding="0" style="font-family:Arial, Helvetica, sans-serif; font-size:10px;">
							  <tr>
								<td width="153"><p><strong>FIRST NAME.................</strong></p></td>
								'.$pFirst.'
							  </tr>

							</table>';


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

$i=1;

foreach ( $listPilots as $Pilot ) {

$pFirst .= '<td width="153"><p>'.$Pilot->pilot_fname.'</p></td>';

if ($i%4==0){$pFirst.='</tr><tr>';}

$i++;

}

 

Use the modulus operator, if we have no remainder the number is divisible by 4 so we make a new table row.

 

HTH

Teamatomic

 

Link to comment
https://forums.phpfreaks.com/topic/192813-math-problem/#findComment-1015691
Share on other sites

$i=1;

foreach ( $listPilots as $Pilot ) {

$pFirst .= '<td width="153"><p>'.$Pilot->pilot_fname.'</p></td>';

if ($i%4==0){$pFirst.='</tr><tr>';}

$i++;

}

 

I would like to loop through a database and get a "chuck" of data then loop through again get another chunk of data until the total number is reached.

 

Is teamatomic solution really going to do what I want it to?  I don't think so.  Or I'm not getting it for some reason.  I need two completely different tables with completely different data and it could be as many as 5 different tables depending on the number of pilots.  Can someone help me understand this way of output?  If I have say 16 pilots with their data in the array then I would need to create 4 separate tables with that data.  Can someone please help with my initial question (problem).  Thanks so much.  Here is the current code.  Notice the table output.  I'm sorry for being a pain in the ***.  :shy:  I attached an image to give an idea of how the output would look.

 


		$offset = 0;
		$loopTimes = floor($totalPilots / 4);

			$sql = 'SELECT * FROM pilots WHERE pilot_aircraft=' . $Aircraft->aircraft_id . ' LIMIT '.$offset.', 4' ;
			$listPilots = pilotData::find_by_sql($sql);

			foreach ( $listPilots as $Pilot ) {

					$pilotCount .= '<td width="153"><p><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>';								

			}

			$pilotTable .= '<table border="0" cellspacing="0" cellpadding="0" width="100%" >
							  <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>';

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/192813-math-problem/#findComment-1016157
Share on other sites

But will it handle 16 pilots or will it only handle 8?

 

...? Here let me give you the pseudo-code

<table>
$pilotcount = 1;
foreach(yadayada) {
      table code
      if($pilotcount%4 == 0){
             </table><table>
      } 
      $pilotcount++;
}
</table>

 

This will create a new table for every 4 pilots

Link to comment
https://forums.phpfreaks.com/topic/192813-math-problem/#findComment-1016164
Share on other sites

Oh i just looked at the image. That's ugly. Why would you want the table listed like that? Anyways if you want it like that you are doing the right thing. Concatenating the strings in ur foreach loop. What you need to do is put the $pilotcount%4 if statement in that foreach and put the entire table code in that if statement. I'll post the code in a second

Link to comment
https://forums.phpfreaks.com/topic/192813-math-problem/#findComment-1016175
Share on other sites

				$sql = 'SELECT * FROM pilots WHERE pilot_aircraft=' . $Aircraft->aircraft_id;
			$listPilots = pilotData::find_by_sql($sql);
			$pc=1;
			foreach ( $listPilots as $Pilot ) {

					$pilotCount .= '<td width="153"><p><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>';								
					if($pc%4==0) {
						echo '<table border="0" cellspacing="0" cellpadding="0" width="100%" >
							  <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>';
					$pFirst = '';
					$pLast = '';
					$pAge = '';
					$pMed = '';
					}
			}
			if($pc%4!=0) {
				echo '<table border="0" cellspacing="0" cellpadding="0" width="100%" >
							  <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>';
			}

Link to comment
https://forums.phpfreaks.com/topic/192813-math-problem/#findComment-1016178
Share on other sites

inspireddesign:

you are making it so much more complicated than it need be. No matter how many pilots you have one table will suffice. You just need to manage the output in a more logical manner using cells and a new spacer row to separate each fourth pilot.

<table>
$i=1;
foreach(blah)
{
if($i==1)
{echo <tr><td>Pilot First<br>Pilot Last<br>Pilot Birth<br>Pilot med</td>}
echo <td>$Pilot->pilot_fname<br>$Pilot->pilot_lname<br>$Pilot->pilot_dob<br>$Pilot->pilot_cert_med</td>
if ($i%4==0)
{
echo </tr><tr><td colspan=5><P> </td></tr>
$i=0;
}
$i++;
}
</table>

 

 

HTH

Teamaotmic

Link to comment
https://forums.phpfreaks.com/topic/192813-math-problem/#findComment-1016179
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.