【问题标题】:pagination in phpphp中的分页
【发布时间】:2011-01-04 20:20:43
【问题描述】:

我正在使用它来收集为特定用户制作的 cmets。我想在每页上显示 7 个 cmets 并希望启用分页。 实施步骤是什么。对不起。 n00b 问题。

$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 查询会将您的网站暴露给 XSS 漏洞。一个好的做法是使用 [[PHP Doctrine Project]] 作为数据库交互层或任何其他好的框架。认为最好从编写查询开始,就像您出于学习目的所做的那样。

标签: php sql pagination


【解决方案1】:

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

这个想法是将返回的结果数量限制为要显示的实际数量。如果您显示第 1 页(共 200 页),每页有 100 个结果,它可以产生巨大的性能提升。

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

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

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

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

<?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");

?>

【讨论】:

    【解决方案2】:

    虽然作为新手学习分页是一项值得努力的工作,但我强烈建议只使用已经编写的代码。查看 PEAR、Zend Framework 或任何其他库集合以实现分页。

    上述示例很好地解释了分页的工作原理,但完全忽略了安全实践、可维护性和其他影响生产质量代码的因素。我不是在抨击上述评论者,我是说他们没有时间为您编写生产质量代码,因此不要使用他们的示例(复制粘贴)。

    安全性,通过清理通过您的 GET 参数传入的内容和安全的数据库抽象层在这里是非常必要的。您还必须处理是否有人手动输入超出范围或低于范围的值的边缘情况。

    目前大多数上述问题都由框架(如 KohanaPHP、Zend Framework、CakePHP 等)处理。

    【讨论】:

      【解决方案3】:

      这是实现分页的基本方法:

      // Page number, >= 1, could come from $_GET for example
      $page = 1;
      // Amount of items to show on page
      $perpage = 7;
      // Calculate the index of first item to show,
      // on page 1 we obviously want to start from 0, hence the - 1 part
      $start = ($page - 1) * $perpage;
      // Build the query using those variables
      $query = "SELECT ... LIMIT $start, $perpage";
      

      您当然必须通过首先检查表中的项目数并调整页面变量等来考虑最大和最小页码,但这应该可以帮助您入门。

      【讨论】:

        【解决方案4】:

        使用 LIMIT x, y 表示要作为一页拉取的第一条记录和记录数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多