goltoof Posted January 14, 2010 Share Posted January 14, 2010 I'm modding a joomla module that displays a bunch of category titles in a row. Instead of it displaying all the titles in one row in one column I want it to break off after 5 or so and display the next 5 in another column and so on. What changes would I need to make to this block to accomplish this? <?php if ($this->params->get('se_toc_columns') == 2) : ?> <td class="setdtoc"> <ul> <?php $nLim = (int) (count( $this->categories ) / 2 + 0.5); for ($i=0, $n=count( $this->categories ); $i < $nLim; $i++) { echo $this->_getHtmlTocEntry($i); } ?> </ul> </td> <?php endif; ?> Additionally it would be great if I could get the list to break off into a new row after 20 links have been displayed (there are about 20 titles for it to display, but I might be adding more). But the first requirement is more important. Quote Link to comment https://forums.phpfreaks.com/topic/188514-breaking-of-a-list-into-columns/ Share on other sites More sharing options...
RussellReal Posted January 15, 2010 Share Posted January 15, 2010 lol what.. I didn't understand one word.. do you understand the difference between rows and columns.. row is left to right.. and column is top to bottom Quote Link to comment https://forums.phpfreaks.com/topic/188514-breaking-of-a-list-into-columns/#findComment-995307 Share on other sites More sharing options...
x_filed2k Posted January 15, 2010 Share Posted January 15, 2010 I'm modding a joomla module that displays a bunch of category titles in a row. Instead of it displaying all the titles in one row in one column I want it to break off after 5 or so and display the next 5 in another column and so on. What changes would I need to make to this block to accomplish this? <?php if ($this->params->get('se_toc_columns') == 2) : ?> <td class="setdtoc"> <ul> <?php $nLim = (int) (count( $this->categories ) / 2 + 0.5); for ($i=0, $n=count( $this->categories ); $i < $nLim; $i++) { echo $this->_getHtmlTocEntry($i); } ?> </ul> </td> <?php endif; ?> Additionally it would be great if I could get the list to break off into a new row after 20 links have been displayed (there are about 20 titles for it to display, but I might be adding more). But the first requirement is more important. Try this... Do you know modulus division? I thinks this is what you are trying to do. <?PHP $no_of_columns = 5; $variable_array = $your_array_variable; $column_begin = "<tr>"; $column_end = "</tr>"; echo("<table border=\"1\">"); for($i = 0; $i < count($variable_array); $i++) { if(($i % $no_of_columns) == 0) echo($column_begin); echo("<td>{$variable_array[$i]}</td> "); if(($i % $no_of_columns) == ($no_of_columns - 1)) echo($column_end); } if(($remaining = ($no_of_columns - ($i % $no_of_columns))) != 0) { for($a = 0; $a < $remaining; $a++) { echo("<td> </td>"); } echo($column_end); } echo("</table>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/188514-breaking-of-a-list-into-columns/#findComment-995314 Share on other sites More sharing options...
goltoof Posted January 15, 2010 Author Share Posted January 15, 2010 lol what.. I didn't understand one word.. do you understand the difference between rows and columns.. row is left to right.. and column is top to bottom Yes... I do know the difference between columns and rows :| Perhaps a visual will help you better understand: The module creates a list like this from $i++ and adds a break after each item so it's all in one <td> (column): <tr> <td> Category 1 Category 2 Category 3 Category 4 Category 5 Category 6 Category 7 Category 8 Category 9 Category 10 Category 11 Category 12 Category 13 Category 14 Category 15 Category 16 Category 17 Category 18 Category 19 Category 20 </td> </tr> (obviously this is all in a table) Instead I want it to look like this: <tr> <td> <td> <td> <td> Category 1 Category 6 Category 11 Category 16 Category 2 Category 7 Category 12 Category 17 Category 3 Category 8 Category 13 Category 18 Category 4 Category 9 Category 14 Category 19 Category 5 Category 10 Category 15 Category 20 </td> </td> </td> </td> </tr> <br><br> <tr> <td> Category 21 Category 22 etc.... </td> </tr> You see Categories 1-5 are in one <td> (one column) Categories 6-10 goes in the next column, etc, until it reaches 20 and breaks off into a new <tr> (a row) and the loop continues. I see your confusion, you're thinking that Category 1, 6, 11 and 16 are one row but that's not necessary. It could be set either way, sure, but it should be easier to just break off each 5 into a new <td> until 20 and then a new <tr> because it already adds a break after each list item. This is just a snippet of an enormously complex joomla module that I have no interest in making any more complex than it already is, except for what I'm trying to accomplish here, of course. Make sense.. at all? Quote Link to comment https://forums.phpfreaks.com/topic/188514-breaking-of-a-list-into-columns/#findComment-995429 Share on other sites More sharing options...
goltoof Posted January 15, 2010 Author Share Posted January 15, 2010 One thing I forgot to include is that it's all in a list, as you can see in the original code I posted. So the output should be more like this: <tr> <td> <td> <td> <td> <ul> <ul> <ul> <ul> Category 1 Category 6 Category 11 Category 16 Category 2 Category 7 Category 12 Category 17 Category 3 Category 8 Category 13 Category 18 Category 4 Category 9 Category 14 Category 19 Category 5 Category 10 Category 15 Category 20 </ul> </ul> </ul> </ul> </td> </td> </td> </td> </tr> <br><br> <tr> <td> </ul> Category 21 Category 22 etc.... </ul> </td> </tr> the <table> tags aren't needed because they're already generated elsewhere. Quote Link to comment https://forums.phpfreaks.com/topic/188514-breaking-of-a-list-into-columns/#findComment-995436 Share on other sites More sharing options...
goltoof Posted January 15, 2010 Author Share Posted January 15, 2010 Does this make sense to anyone? It seems like it should be quite simple the script already works just needs a small fix to break it off into new columns. Quote Link to comment https://forums.phpfreaks.com/topic/188514-breaking-of-a-list-into-columns/#findComment-995664 Share on other sites More sharing options...
laffin Posted January 15, 2010 Share Posted January 15, 2010 <?php $list_items=32; $items_per_cell=5; $max_cols=5; for($i=0;$i<$list_items;$i++) $category[$i]="Category{$i}"; $cols=($list_items/$items_per_cell) + ($list_items % $items_per_cell?1:0); $rows=($cols/$max_cols) +($cols%$max_cols?1:0); $cur_item=0; echo "<table>"; for($i=0;$i<$rows;$i++) { if($i) echo "<br><br>"; echo "<tr>\n"; for($j=0;$j<$max_cols;$j++) { echo "<td><ul>\n"; for($k=$cur_item;$k<($cur_item+5);$k++) { if($k<$list_items) echo "<li>{$category[$k]}</li>\n"; else echo " "; } $cur_item=$k; echo "</ul></td>\n"; } echo "</tr>\n"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/188514-breaking-of-a-list-into-columns/#findComment-995684 Share on other sites More sharing options...
goltoof Posted January 22, 2010 Author Share Posted January 22, 2010 got it working with this: <?php $no_of_columns = 3; $row_begin = "<tr class=\"setrtoc\">\n"; $row_end = "</tr>"; for ($i=0, $n=count( $this->categories ); $i < $n; $i++) { if(($i % $no_of_columns) == 0) echo($row_begin); echo "<td class=\"setdtoc\">\n"; echo $this->_getHtmlTocEntry($i); echo "</td>\n"; if(($i % $no_of_columns) == ($no_of_columns - 1)) echo($row_end); } ?> Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/188514-breaking-of-a-list-into-columns/#findComment-999939 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.