Jump to content

long lists in columns


bwakad
 Share

Recommended Posts

Not sure if someone already have this, but I just wanted to share it, hoping someone improve it, maybe it's good for a function?

It will split long list of pages first top to bottom, then left to right. columns are chosen by you.

A | D | G

B | E | H

C | F | I

$columns = 3;  // choose number of columns  
$item = 0; // set counter to zero
$num = $pages->count("parent=/branche/"); // use variable for total pages
$itemsInColumn = intval($num / $columns); // calculate how many child-pages per column
echo "total: ". $num . " | per column: " . $itemsInColumn . "<br />"; // this can be removed if you don't want
echo "<div class='left'>"; // left is css: float left
foreach($pages->find("parent=/branche/") as $child) {
   $item ++; // count for each child-page a plus one
      if($item > $itemsInColumn) { $item = 1; // set back to 1 if total is larger then total in column
         echo "</div>"; // then end div
         echo "<div class='left'>"; // and start new float left
      }
   echo "<a href='{$child->url}'>{$child->title}</a><br />"; // output my child-page
}
echo "</div><div class='clear'></div>"; // clear float, end div

I know you all must have this, but I am really starting to be afraid asking ;) so I found out by myself

  • Like 8
Link to comment
Share on other sites

  • 2 months later...

Very useful piece of code indeed, thank you! It kind of goes haywire if $itemsInColumn is uneven though.

Using ceil($num / $columns); instead of intval($num / $columns); fixed it for me!

cheers

phil

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...