views:

31

answers:

2

I have a users table which I want to display like the stackoverflow users page. So say display 5 records then take a new row, display 5 records, take a new row...

What's the best way to achieve this?

Thanks,

Billy

+1  A: 

You are talking about something called pagination. You can achieve it by having a variable that indicated page number and based on it calculate which rows and from which one you will be displaying the data.

Tomasz Kowalczyk
thanks it seems that cakephp has a pagination helper that i'll use
iamjonesy
oh, i didn't see that you tagged it as cakephp - i personally use symfony, but yes, cake has this functionality too.
Tomasz Kowalczyk
+1  A: 

If you want to do something every X iterations you'll need the modulo operator %

So, lets say you're using table rows and you want a new one every 5 items displayed. You'll basically have the items inside a <tr>, each one in its own <td>. Then, every 5 items you'll close the row and open a new one:

$str = <<< END
<table>
    <tbody>
        <tr>
END;

$numItems = count($items) ;

for ($i = 0 ; $i < $numItems) ; $i++) {
    $currItem = $items[$i] ;
    $str .= "<td>$currItem</td>";

    if ($i % 5 == 0) {
        $str .= "
        </tr>
        <tr>" ;
    }
}

$str .= "
        </tr>
    </tbody>
</table>" ;
Fanis
Sorry I took so long, this looks good thanks! I'm going to try it out
iamjonesy