开发者

SQL - adding result page numbers based on number of results?

I have a SQL query that separates the results into groups of 25:

$page = 0; // 1, 2, 3
$perPage = 25;

....

ORDER BY c.date DESC LIMIT " . ( $page * $perPage ) . ", " . $perPage);

I'm trying to figure out how I write something t开发者_JS百科hat loops through the number of $page returned and then echos a number back to the screen. Ie. Page 1, Page2, Page3 - based on $page.


I will assume the search PHP page is driven throught GET parameters like page like a.php?page=5 or something.

In that case, try something of this sort:

$total = //total no. of rows. if using mysql use: mysql_numrows($resource);
$total = ceil($total/$perPage);
for ($i=0; $i<$total; $i++){
    echo '<a href="' . $_SERVER['PHP_SELF'] . '?page=' . $i . '">Page ' . ($i+1) . '</a>';
}

This is just to give you a kick start. You can obviously, take this ahead in the way you would like.

What you are doing is called Pagination. Just google for PHP Pagination, and you will end up with lots of results. And most of them are just too easy to use.

Try Zend Paginator: http://framework.zend.com/manual/en/zend.paginator.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜