在PHP中分页 [英] pagination in php

查看:88
本文介绍了在PHP中分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此工具收集特定用户的评论.我想在每个页面上显示7条评论,并希望启用分页. 实施步骤是什么?对不起. n00b问题.

i am using this to collect comments made abt a particular user. i want to display 7 comments on each page and want to enable pagination. what would be the steps of implementation. sorry. n00b question.

$query = "SELECT msg, user_id, comment_time FROM comments WHERE aid = '$aid' ORDER BY comment_time DESC";
        $result = mysql_query($query) or die("ERROR: $query.".mysql_error());
        if (mysql_num_rows($result) > 0) {
            while($row = mysql_fetch_object($result)){
                $uidval = $row->user_id;
                if ($uidval != 0){
                $userInfo = $facebook->api_client->fql_query("SELECT name, pic_square_with_logo, profile_url FROM user WHERE uid='".$uidval."'");

            //  echo '<br>Array Info<br>';
            //  print_r($userInfo);
            //  echo '<br><br>';

                $nameuser = $userInfo[0]['name'];
                $pic = $userInfo[0]['pic_square_with_logo'];
                $profile_url = $userInfo[0]['profile_url'];

                echo '<img src="'.$pic.'" />';
                echo '<a href="'.$profile_url.'">'.$nameuser.'</a>';
                echo '<br>';
                echo $row->comment_time;
                echo '<br>';
                echo $row->msg;
                echo '<br>';
                }

            }
        }

推荐答案

该解决方案最好通过SQL的limit/offset子句实现.对于MySQL,这是通过将LIMIT [offset] [count]附加到查询中来实现的. PostgreSQL使用单独的select ... LIMIT [count] OFFSET [offset]语法.

The solution is best achieved via SQL's limit/offset clauses. For MySQL, this is achieved via appending LIMIT [offset] [count] to your query. PostgreSQL uses separate select ... LIMIT [count] OFFSET [offset] syntax.

想法是将返回的结果数限制为要显示的实际数.如果您显示200页的第1页,每页一百个结果,则可以显着提高性能.

The idea is you limit the number of results returned to the actual number you want to display. It can yield a huge performance increase if you're displaying page 1 of 200, with a hundred results per page.

当然,您需要运行第二个查询-select count(*) from ...以确定结果集中结果的数量.将其除以每页的结果数,便得到了页面数.

Of course, you need to run a second query - a select count(*) from ... to determine the number of results in the result set. Divide this by the number of results per page, and you've got the number of pages.

您可能希望在查询字符串中传递一个页面参数,例如

You will likely want to pass a page parameter in the query string, something like

http://mysite.com/index.php?page=7

,然后使用类似的方法提取数据(考虑此伪代码;我知道我实际上没有正确查询)

and then pull the data using a similar method (consider this pseudo code; I know I'm not actually querying properly)

<?php

$num_per_page = 20; // 20 results per page

$page = isset($_GET['page']) ? $_GET['page'] : 0; // start from page 0

// Build our big query minus the SELECT part here
$from_part = 'tbl_results [where ...]"'

$num_results = query("SELECT COUNT(*) FROM $from_part");

// Use ceil to round upwards
$num_pages = ceil($num_results / $num_per_page);

// Cap the page a the last page
if ($page > $num_pages)
  $page = $num_pages;

// Cap the page at the first page
if ($page <= 1)
  $page = 1;

$offset = $page * $num_per_page;

// Build the final query to select the relevant page of data
$results = query("SELECT * FROM $from_part LIMIT $offset, $num_per_page");

?>

这篇关于在PHP中分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆