tags:

views:

29

answers:

1

i have a list of returned rows from mysql that i am outputting using php:

          echo '<ul class="mylist">';
    foreach ($rows as $row)
    {
        echo '<li><a href="'.$row->url.'" target="_blank">' . $row->title . '</a></li>';
    }
    echo  "</ul>";

works fine, but its a long list and i would like to split it into ul chunks so that i can make columns. maybe like 5 results per ul. instead of one ul...

i tried wrapping in a for statement but then just wound up outputting the results 5 times...oops...

+2  A: 

Take a look at array_chunk:

foreach (array_chunk($rows, 5) as $chunk)
{
  echo '<ul class="mylist">';
  foreach ($chunk as $row)
  {
     echo '<li><a href="'.$row->url.'" target="_blank">' . $row->title . '</a></li>';
  }
  echo '</ul>';
}
Daniel Vandersluis
This may just be me but I think you really shouldn't be doing loops within loops like that; especially if there's a way to avoid it.
Eric Lamb
@Eric The number of iterations are the same in your answer and in mine; here it is just splitting rows into groups of 5 first.
Daniel Vandersluis
this seems pretty efficient to me.. thanks!
liz
I get that; I was speaking of personal preference only :)
Eric Lamb