Jump to content

breaking of a list into columns


goltoof

Recommended Posts

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.

Link to comment
Share on other sites

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>");





?>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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>";
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.