mac007 Posted November 23, 2010 Share Posted November 23, 2010 hello, all: I'm a newbie, and been trying to work out a way so that the results from an array (or mysql recordset for that matter), are nicely aranged in a table. I want them to show in a grid-like manner, with rows and columns added according to number of "items". I worked out this small code snippet, with a sample array, and for the life of me, cant figure out how to make it so it automatically "breaks" columns at 5 and start new row. I got it to show me right number of rows, but then it repeats all 10 names within them, as opposed to just showing 5 names in each row... Appreciate the help... <?php $names = array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia'); $numberColumns = 5; $numberNames = count($names); $numberRows = ceil($numberNames / $numberColumns); echo "<table border='1' width='400' cellspacing='0' cellpadding='0'>"; for ($i=1; $i<=$numberRows; $i++) { echo "<tr>"; foreach ($names as $name) { echo "<td>" . $name . "</td>"; } echo "</tr>"; } echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/219608-dinamically-add-rowscolumns-to-table-depending-on-number-of-results/ Share on other sites More sharing options...
BlueSkyIS Posted November 23, 2010 Share Posted November 23, 2010 i'm not sure this is what you're after, but this is what i use to place data in rows instead of columns: /// PUT DATA IN ROWS INSTEAD OF COLUMNS ////////// $items = array('ALABAMA','ALASKA');//create an array with your data // Default # of Columns $numcols = 4; // Number of Items $numitems = count($items); // Number of Rows $numrows = ceil($numitems/$numcols); echo '<table>'; for ($row=1; $row <= $numrows; $row++) { $cell = 0; echo ' <tr>'."\n"; for ($col=1; $col <= $numcols; $col++) { echo ' <td>'."\n"; if ($col===1) { $cell += $row; print $items[$cell - 1]; } else { $cell += $numrows; print $items[$cell - 1]; } echo ' </td>'."\n"; } echo ' </tr>'."\n"; } echo '</table>'; Link to comment https://forums.phpfreaks.com/topic/219608-dinamically-add-rowscolumns-to-table-depending-on-number-of-results/#findComment-1138582 Share on other sites More sharing options...
mac007 Posted November 23, 2010 Author Share Posted November 23, 2010 You are awesome... I see I was going in the right direction, but all those for loops pretty much turned me into a pretzel! Gonna have to study your code carefully here and see how in the heck the cells get embedded in there.. Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/219608-dinamically-add-rowscolumns-to-table-depending-on-number-of-results/#findComment-1138586 Share on other sites More sharing options...
Andy-H Posted November 23, 2010 Share Posted November 23, 2010 <?php $names = array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia'); $numberColumns = 5; $names = array_chunk($names, $numerColumns); $k = count($names) - 1; $names[$k] = array_pad($names[$k], 5, ''); echo "<table border='1' width='400' cellspacing='0' cellpadding='0'>"; foreach($names as $k => $v) { echo '<tr><td>' . implode('</td><td>', $names) . '</td></tr>'; } echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/219608-dinamically-add-rowscolumns-to-table-depending-on-number-of-results/#findComment-1138587 Share on other sites More sharing options...
mac007 Posted November 23, 2010 Author Share Posted November 23, 2010 wow, Andy, you threw php functions in there that I had never seen before... very interesting. I tried your code but didnt seem like it worked... didnt seem to output anything. Gotta say, a very different way to approach the problem.. never seen it addressed this way Link to comment https://forums.phpfreaks.com/topic/219608-dinamically-add-rowscolumns-to-table-depending-on-number-of-results/#findComment-1138596 Share on other sites More sharing options...
mac007 Posted November 23, 2010 Author Share Posted November 23, 2010 Hey Blue... was looking over your code... how come you are putting "/n" in there?? does it make a differece, just asking; found it odd. It doesnt seem to affect it either way.. thanks! Link to comment https://forums.phpfreaks.com/topic/219608-dinamically-add-rowscolumns-to-table-depending-on-number-of-results/#findComment-1138602 Share on other sites More sharing options...
BlueSkyIS Posted November 23, 2010 Share Posted November 23, 2010 the code contained the newline breaks when I copied it. Link to comment https://forums.phpfreaks.com/topic/219608-dinamically-add-rowscolumns-to-table-depending-on-number-of-results/#findComment-1138663 Share on other sites More sharing options...
mac007 Posted November 23, 2010 Author Share Posted November 23, 2010 gotcha... Link to comment https://forums.phpfreaks.com/topic/219608-dinamically-add-rowscolumns-to-table-depending-on-number-of-results/#findComment-1138687 Share on other sites More sharing options...
Andy-H Posted November 25, 2010 Share Posted November 25, 2010 <?php $names = array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia'); $numberColumns = 5; /* the following line chunks the array into a multi-dimentional array with each sub-array containing 5 elements see below. */ $names = array_chunk($names, $numberColumns); /* the following two lines pas the last sub-array to five values, filling any extra values with an empty string '' */ $k = count($names) - 1; $names[$k] = array_pad($names[$k], 5, ''); echo "<table border='1' width='400' cellspacing='0' cellpadding='0'>\r\n"; foreach($names as $v) { echo "\t<tr>\r\n\t\t<td>" . implode("</td>\r\n\t\t<td>", $v) . "</td>\r\n\t</tr>\r\n"; } echo "</table>"; ?> array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia') is chunked into array ( [ 0 ] => array ('Charles', 'Henry', 'Manny', 'Philip', 'Rose') [ 1 ] => array ('Evelyn', 'Peter', 'Julia', 'Cary', 'Sophia') ) My bad, works now, tested and outputted: <table border='1' width='400' cellspacing='0' cellpadding='0'> <tr> <td>Charles</td> <td>Henry</td> <td>Manny</td> <td>Philip</td> <td>Rose</td> </tr> <tr> <td>Evelyn</td> <td>Peter</td> <td>Julia</td> <td>Cary</td> <td>Sophia</td> </tr> </table> array_chunk array_pad count implode Note, \r\n outputs a carrige return (newline) and \t outputs a tab spacer. It's for the formatting of the HTML. Link to comment https://forums.phpfreaks.com/topic/219608-dinamically-add-rowscolumns-to-table-depending-on-number-of-results/#findComment-1139706 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.