开发者

How to layout records horizontally and vertically

开发者 https://www.devze.com 2023-01-15 15:26 出处:网络
I have a users table which I want to display like the Stack Overflow users page. So say开发者_运维技巧 display 5 records then take a new row, display 5 records, take a new row...

I have a users table which I want to display like the Stack Overflow 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?


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消