【问题标题】:Pagination while using a GET variable使用 GET 变量时的分页
【发布时间】:2009-09-18 08:27:44
【问题描述】:

我很难对下面的代码进行分页。我认为这与将 GET 变量 $find 传递到下一页有关。

无论如何,我将如何对下面的代码进行分页,以便下表每页仅显示 100 行?

提前致谢,

约翰

<?php
ob_start();
session_start();
$find = strip_tags($_GET['find']);
$illegal = array("'", ".", "/", "\"", ";", "{", "}", "[", "]", "\\", "''", "'''", "''''", "'''''", "\\\\", "\\\\\\", "\\\\\\\\");
$find = str_replace($illegal, '', $find);
$find = trim ($find);
$find = strtolower($find);
$find = stripslashes($find);
$_SESSION['find'] = $find;
?>

<?
if ($searching =="yes")
{

if ($find == "")
{
session_write_close();
header("Location:http://www.site.com/index.php");
exit;
unset($_SESSION['find']);   

}

mysql_connect("mysqlv10", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$find = mysql_real_escape_string($find);

$result=mysql_query("SHOW TABLES FROM database LIKE '$find'")
or die(mysql_error());

if(mysql_num_rows($result)>0){
while($table=mysql_fetch_row($result)){
print "<p class=\"topic\">$table[0]</p>\n";
$r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` WHERE site != '' ORDER BY effective_vote DESC");

print "<table class=\"navbar\">\n";
while($row=mysql_fetch_array($r)){

$effective_vote = $row['votes_up'] - $row['votes_down']; 

print "<tr>";

print "<td class='sitename'>".'<a type="amzn" category="products" class="links2">'.$row['site'].'</a>'."</td>";

print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>";
print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.vote.'</a>'.'</span>'."</td>";
}
print "</tr>\n";
}
print "</table>\n";

}
?>

【问题讨论】:

    标签: php mysql pagination


    【解决方案1】:

    您必须在查询中使用LIMIT 来告诉数据库您想要多少行以及从哪里开始。

    传递一个参数,告诉脚本您想要另一块结果,而不仅仅是第一批。

    所以对于链接,您可以传递page 参数:

    example.com/results.php?page=2
    

    page= 将告诉脚本您要返回哪个页面。

    然后您需要LIMIT 每次返回的行数,以便您有一致的分页。

    $results_cnt = 100; //--rows you want per page of results
    

    现在在您的脚本中,您将检查是否已设置 page 变量。如果不是,则默认开始行从第一行返回。但是,当您想要返回不同的页面/结果集时,需要进行一些数学运算才能从正确的行开始。

    if(isset($_GET["page"]) //--see if the variable is even there
    {
        $page_num = (int)$_GET["page"]; //--forcing it to always be an integer
    
        $start_row = $results_cnt * ($page_num - 1);
    
        /* --
         what happens:
           ($results_cnt currently at 100)
    
         on page one (page=1), start at row 0
          math: 100 * (1 - 1) = 0
    
         on page two (page=2), start at row 100
          math: 100 * (2 - 1) = 100
    
         on page three (page=3), start at row 200
          math: 100 * (3 - 1) = 200
    
         etc.
        */
    }
    else
        $start_row = 0;
    

    现在,已经设置了正确的起始行,调整 SQL 查询以使用如下变量:

    $r = mysql_query("SELECT *, votes_up - votes_down AS effective_vote 
                       FROM `$table[0]`
                       WHERE site != '' 
                       ORDER BY effective_vote DESC
                       LIMIT $start_row, $results_cnt");
    

    每次您点击该页面时,它都会检查$_GET["page"] 是否存在。如果不是,则从第一行开始显示。如果是,请进行数学计算并计算出要跳过多少行并显示下一页。

    【讨论】:

      【解决方案2】:

      你需要使用

      LIMIT (<pagenumber*amount of records you want to display>,< amount of records you want to display >)
      

      在您的 SQL 语句中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-12
        • 2013-06-19
        • 2011-01-04
        • 2016-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多