benphp Posted December 29, 2009 Share Posted December 29, 2009 Say you have an $arrayItem = Item01, Item02, Item03, Item04, Item05, Item06, Item07, Item08, Item09, Item10, Item11, Item12. I want to display it in a table so that they are sorted down and across rather than just across: Item01 Item04 Item07 Item09 Item11 Item02 Item05 Item08 Item10 Item12 Item03 Item06 and it varies: Item01 Item05 Item09 Item13 Item16 Item02 Item06 Item10 Item14 Item17 Item03 Item07 Item11 Item15 Item18 Item04 Item08 Item12 ?? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/ Share on other sites More sharing options...
benphp Posted December 30, 2009 Author Share Posted December 30, 2009 Here's what I've got so far. This is tricky. <?php $aLine = array('Item01', 'Item02', 'Item03', 'Item04', 'Item05', 'Item06', 'Item07', 'Item08', 'Item09', 'Item10', 'Item11', 'Item12', 'Item13', 'Item14', 'Item15', 'Item16', 'Item17', 'Item18', 'Item19'); ////tabe 3 print " <table> <tr>\n"; $cols = 5; $colCount = 1; $numElements = count($aLine); $rows = round($numElements/$cols); $i = 0; $rcount = 1; while($i < $numElements) { $increment = $i * $rows; if ($increment <= $numElements) { $cell = $aLine[$increment]; if ($colCount < $cols) { print "\t<td>$cell </td>\n"; } else if ($colCount == $cols) { print "\t<td>$cell </td>\n</tr> \n"; } $colCount ++; } $i++; } print "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-985714 Share on other sites More sharing options...
.josh Posted December 30, 2009 Share Posted December 30, 2009 you have the general principle but I think you're over-complicating it. $aLine = array('Item01', 'Item02', 'Item03', 'Item04', 'Item05', 'Item06', 'Item07', 'Item08', 'Item09', 'Item10', 'Item11', 'Item12', 'Item13', 'Item14', 'Item15', 'Item16', 'Item17', 'Item18', 'Item19'); $numCols = 5; echo "<table><tr>"; foreach($aLine as $key => $val) { echo ($key % $numCols == 0)? "</tr><tr>" : ""; echo "<td>$val</td>"; } echo "</tr></table>"; Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-985722 Share on other sites More sharing options...
benphp Posted December 30, 2009 Author Share Posted December 30, 2009 That works great for listing it across (modulus works for that too), but what about down? Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-985724 Share on other sites More sharing options...
.josh Posted December 30, 2009 Share Posted December 30, 2009 what do you mean down instead of across? How many rows you end up with will depend on how many elements are in your array. Are you saying you want to be able to set how many rows there will be, but have a variant number of columns? First off, I have to ask why would you possibly want to do that? Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-985730 Share on other sites More sharing options...
cags Posted December 30, 2009 Share Posted December 30, 2009 No, the OP is saying they wish a predefined number of columns, with whatever amount of rows that determines. But they want them to be 'dealt' downwards first. As shown in their examples. I'm not entirely sure why it would really matter, but it is an interesting problem. I think I may have a solution, which I will paste after testing it. Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-985735 Share on other sites More sharing options...
.josh Posted December 30, 2009 Share Posted December 30, 2009 No, the OP is saying they wish a predefined number of columns, with whatever amount of rows that determines. But they want them to be 'dealt' downwards first. As shown in their examples. I'm not entirely sure why it would really matter, but it is an interesting problem. I think I may have a solution, which I will paste after testing it. ah I missed that.... actually I vaguely recall solving this a long time ago in some previous post. will have to search. Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-985743 Share on other sites More sharing options...
cags Posted December 30, 2009 Share Posted December 30, 2009 Well here's my offering for what it's worth, there might very well be a simpler way but... // predefine variables $items = range(1, 12); // your actual array of items here $col_count = 5; // calculated variables $item_count = count($items); $row_count = ceil($item_count/$col_count); $total_squares = $row_count * $col_count; $full_cols = $col_count - ($total_squares % $item_count); echo '<table>'; for($row = 0; $row < $row_count; $row++) { echo '<tr>'; for($col = 0; $col < $col_count; $col++) { if($col < $full_cols) { $index = ($row + 1) + ($row_count * $col); } else { if($row == ($row_count - 1)) { $index = -1; } else { $index = ($row + 1) + ($full_cols * $row_count) + (($col - $full_cols) * ($row_count - 1)); } } if(isset($items[$index - 1])) { echo '<td>' . $items[$index - 1] . '</td>'; } else { echo '<td> </td>'; } } echo '</tr>'; } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-985750 Share on other sites More sharing options...
benphp Posted December 30, 2009 Author Share Posted December 30, 2009 cags - that's the ticket - quite a bit of magic going on there. Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-985763 Share on other sites More sharing options...
.josh Posted December 30, 2009 Share Posted December 30, 2009 This is my take. I'm pretty sure I can merge a lot of it together; ran out of time. $aLine = array('Item01', 'Item02', 'Item03', 'Item04', 'Item05', 'Item06', 'Item07', 'Item08', 'Item09', 'Item10', 'Item11', 'Item12', 'Item13', 'Item14', 'Item15', 'Item16', 'Item17', 'Item18', 'Item19'); $numCols = 4; $numRows = ceil(count($aLine) / $numCols); $aLine = array_chunk($aLine,$numRows); foreach ($aLine as $key => $val) { foreach ($val as $k => $v) { $taLine[$k][] = $v; } } $aLine = array(); foreach ($taLine as $key => $val) { $aLine = array_merge($aLine, $val); } echo "<table><tr>"; foreach($aLine as $key => $val) { echo ($key % $numCols == 0)? "</tr><tr>" : ""; echo "<td>$val</td>"; } echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-985775 Share on other sites More sharing options...
cags Posted December 30, 2009 Share Posted December 30, 2009 cags - that's the ticket - quite a bit of magic going on there. It was a simple concept, bit of a bitch to wrap my head around getting the maths right though. The basic theory being you calculate based on a grid position how many 'items' have been used already, then insert the next item. It looks like the approach taken by CV is another one I considered, I discounted it as an option because I couldn't work out how to solve a few of the issues with it. Unfortunately I don't think CV fully did either. If you remove item19 from the example it - if you'll excuse the phrase - goes to cock. Of course I'm not saying my solutions perfect, there may be situations where it will fail, I didn't test it that thoroughly. Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-985912 Share on other sites More sharing options...
.josh Posted December 30, 2009 Share Posted December 30, 2009 well damn...didn't figure on that. =\ Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-986044 Share on other sites More sharing options...
benphp Posted December 30, 2009 Author Share Posted December 30, 2009 Thanks everyone! Crayon Violent - that's a neater solution. I wouldn't have been able to do that. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-986045 Share on other sites More sharing options...
.josh Posted December 30, 2009 Share Posted December 30, 2009 well as cags pointed out, mine is broken, so don't use it. Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-986046 Share on other sites More sharing options...
.josh Posted December 31, 2009 Share Posted December 31, 2009 so I was randomly thinking about this, and I came up with a different approach: $list = range(1,20); $numCols = 7; $numRows = ceil(count($list) / $numCols); echo "<div style='float:left'>"; foreach ($list as $c => $item) { echo ($c % $numRows == 0)? "</div><div style='float:left'>" : ""; echo "<div style='border:solid 1px green;padding:2px'>$item</div>"; } echo "</div>"; Basically the idea here is to minimize the scripting (logic) involved in displaying the values, and let css do the formatting work, instead. I mean after all, isn't that what it's there for? There is one tiny "bug" in this that that ceil() kinda makes it want to not be forced to certain column amounts. For instance, trying to set the columns at 6 will make it format as 5 columns. The numbers are still ordered right, though. This may or may not be an issue to you, as you may not need it to be 6 columns. Quote Link to comment https://forums.phpfreaks.com/topic/186631-list-an-array-in-a-table-with-predefined-columns/#findComment-986580 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.