Jump to content

Creating tables using loop


inspireddesign

Recommended Posts

Hi everyone,

 

The below script is supposed to loop through the array and output separate tables based on the amount of data that is being passed to it.  The table is getting generated fine but it's not segmenting the tables in groups of four.  The idea is that every group of four gets their own table.

 

What am I missing?  Can someone help me?  Thanks.

 

$i=1;

foreach($listAI as $ai) {

	$ai_name 		.= '<td width="175">' . $ai->ai_name . '</td>';
	$ai_nie  		.= '<td width="175">' . $ai->ai_nie . '</td>';
	$ai_address  	.= '<td width="175">' . $ai->ai_address . '</td>';
	$ai_loc  		.= '<td width="175">' . $ai->ai_city. ' '.$ai->ai_state. ', ' .$ai->ai_zip . '</td>';
}

if($i==1){

	$aiOutput .= '<table border="0" cellspacing="0" cellpadding="0" style="font-size:8pt;">';
	}
	$aiOutput .= '<tr>'. $ai_name . '</tr>
				  <tr>' . $ai_nie .'</tr>
				  <tr>'. $ai_address . '</tr>
				  <tr>' . $ai_loc . '</tr>';

		if ($i%4==0) {
		$aiOutput .= '</table>';
		$i=0;

		}
	$i++;		

	$aiOutput .= '</table>';

Link to comment
https://forums.phpfreaks.com/topic/193351-creating-tables-using-loop/
Share on other sites

you close off your foreach too early and i dont think you wanted you td vars (ai_name, etc) to concatenate.

 

$i=1;

foreach($listAI as $ai) {

	$ai_name 		= '<td width="175">' . $ai->ai_name . '</td>';
	$ai_nie  		= '<td width="175">' . $ai->ai_nie . '</td>';
	$ai_address  	= '<td width="175">' . $ai->ai_address . '</td>';
	$ai_loc  		= '<td width="175">' . $ai->ai_city. ' '.$ai->ai_state. ', ' .$ai->ai_zip . '</td>';


if($i==1){

	$aiOutput .= '<table border="0" cellspacing="0" cellpadding="0" style="font-size:8pt;">';
	}

	$aiOutput .= '<tr>'. $ai_name . '</tr>
				  <tr>' . $ai_nie .'</tr>
				  <tr>'. $ai_address . '</tr>
				  <tr>' . $ai_loc . '</tr>';

		if ($i%4==0) {
		$aiOutput .= '</table>';
		$i=0;

		}
	$i++;		
}
	$aiOutput .= '</table>';

Thanks for the reply schilly.  Your way does put the output in a table like I want BUT it's not the correct output.  I've been messing with this for days now and I can't seem to figure out what I'm doing wrong.  Your solutions html output puts the data in one column.  What I'm trying to get is four rows and four columns.

 

So the output would look something like the attached image.

 

[attachment deleted by admin]

ok. i see why you wanted to concatenate the fields now. k I think you want:

 

$i=1;

foreach($listAI as $ai) {

	$ai_name 		.= '<td width="175">' . $ai->ai_name . '</td>';
	$ai_nie  		.= '<td width="175">' . $ai->ai_nie . '</td>';
	$ai_address  	.= '<td width="175">' . $ai->ai_address . '</td>';
	$ai_loc  		.= '<td width="175">' . $ai->ai_city. ' '.$ai->ai_state. ', ' .$ai->ai_zip . '</td>';


if($i==1){

	$aiOutput .= '<table border="0" cellspacing="0" cellpadding="0" style="font-size:8pt;">';
	}



		if ($i%4==0) {

$aiOutput .= '<tr>'. $ai_name . '</tr>
				  <tr>' . $ai_nie .'</tr>
				  <tr>'. $ai_address . '</tr>
				  <tr>' . $ai_loc . '</tr>';
		$aiOutput .= '</table>';
		$i=0;

$ai_name = '';
$ai_nie = '';
$ai_address = '';
$ai_loc = '';
		}
	$i++;		
}
	$aiOutput .= '</table>';

 

so basically concatenate the table data until we hit the 4th row. add the data to the output inside the table rows. reset the fields. repeat.

 

that should output how you want i think.

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.