inspireddesign Posted February 25, 2010 Share Posted February 25, 2010 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 More sharing options...
schilly Posted February 25, 2010 Share Posted February 25, 2010 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>'; Link to comment https://forums.phpfreaks.com/topic/193351-creating-tables-using-loop/#findComment-1018119 Share on other sites More sharing options...
inspireddesign Posted February 25, 2010 Author Share Posted February 25, 2010 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] Link to comment https://forums.phpfreaks.com/topic/193351-creating-tables-using-loop/#findComment-1018213 Share on other sites More sharing options...
schilly Posted February 25, 2010 Share Posted February 25, 2010 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. Link to comment https://forums.phpfreaks.com/topic/193351-creating-tables-using-loop/#findComment-1018216 Share on other sites More sharing options...
inspireddesign Posted February 26, 2010 Author Share Posted February 26, 2010 That did it. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/193351-creating-tables-using-loop/#findComment-1018311 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.