filoaman Posted November 24, 2012 Share Posted November 24, 2012 Hi friend I try to create a simple html table and put on every cell data from three arrays my arrays are: $one = array('title1','title2','title3','title4','title5','title6'); $two = array('image1','image2','image3','image4','image5','image6'); $three = array('text1','text','text3','text4','text5','text6'); What i like to create is a table like this: ----------------------------------- | title1 | title2 | title3 | ----------------------------------- | image1 | image1 | image3 | ----------------------------------- | text1 | text2 | text3 | ----------------------------------- | title4 | title5 | title6 | ----------------------------------- | image4 | image5 | image6 | ----------------------------------- | text4 | text5 | text6 | ----------------------------------- In other words i need a script to create a rows take the first 3 data from every array put them on cells and every 3 cells create a new set of rows. Until now i'm able using the following cote to put data from only one array $numFiles = count($one); $start = 0; $end = $numFiles; $split = 3; print "<table><tr>"; for($i = $start; $i < $end; $i++) { print "<td>".$one[$i]."</td>"; if(($i) % ($split) == $split-1){ print "</tr><tr>"; } } print"</tr></table>"; If i use something like this: $numFiles = count($one); $start = 0; $end = $numFiles; $split = 3; print "<table><tr>"; for($i = $start; $i < $end; $i++) { print "<td>".$one[$i]."</td>"; if(($i) % ($split) == $split-1){ print "</tr><tr>"; for($i = $start; $i < $end; $i++) { print "<td>".$two[$i]."</td>"; if(($i) % ($split) == $split-1){ print "</tr><tr>"; } } } } print"</tr></table>"; I get this: ----------------------------------- | title1 | title2 | title3 | ----------------------------------- | image1 | image1 | image3 | ----------------------------------- | image4 | image5 | image6 | ----------------------------------- I know that i'm close but i need some help... Quote Link to comment https://forums.phpfreaks.com/topic/271109-create-html-table-using-php/ Share on other sites More sharing options...
DavidAM Posted November 24, 2012 Share Posted November 24, 2012 Write a single loop, and use the index for all three arrays. Collect each row in a separate variable, and output all three when you get to the split. Something like this: $one = array('title1','title2','title3','title4','title5','title6'); $two = array('image1','image2','image3','image4','image5','image6'); $three = array('text1','text','text3','text4','text5','text6'); $numFiles = count($one); $start = 0; $end = $numFiles; $split = 3; print "<table>"; $titles = $images = $texts = ''; for($i = $start; $i < $end; $i++) { $titles .= "<td>".$one[$i]."</td>"; $images .= "<td>".$two[$i]."</td>"; $texts .= "<td>".$three[$i]."</td>"; if(($i % $split) == $split-1){ print "<tr>" . $titles . "</tr><tr>" . $images . "</tr><tr>" . $text . "</tr>" $titles = $images = $texts = ''; } } // Just in case the count is not a multiple of the split if (! empty($titles)) { print "<tr>" . $titles . "</tr><tr>" . $images . "</tr><tr>" . $text . "</tr>" } print"</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/271109-create-html-table-using-php/#findComment-1394759 Share on other sites More sharing options...
filoaman Posted November 24, 2012 Author Share Posted November 24, 2012 Great Help thank you. I only had to change some typos. The working code is this $one = array('title1','title2','title3','title4','title5','title6'); $two = array('image1','image2','image3','image4','image5','image6'); $three = array('text1','text','text3','text4','text5','text6'); $numFiles = count($one); $start = 0; $end = $numFiles; $split = 3; print "<table>"; $titles = $images = $texts = ''; for($i = $start; $i < $end; $i++) { $titles .= "<td>".$one[$i]."</td>"; $images .= "<td>".$two[$i]."</td>"; $texts .= "<td>".$three[$i]."</td>"; if(($i % $split) == $split-1){ print "<tr>" . $titles . "</tr><tr>" . $images . "</tr><tr>" . $texts . "</tr>"; $titles = $images = $texts = ''; } } // Just in case the count is not a multiple of the split if (! empty($titles)) { print "<tr>" . $titles . "</tr><tr>" . $images . "</tr><tr>" . $texts . "</tr>"; } print"</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/271109-create-html-table-using-php/#findComment-1394764 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.