【问题标题】:What is wrong with this pagination class?这个分页类有什么问题?
【发布时间】:2012-08-02 21:29:01
【问题描述】:

我正在寻找一个简单的分页脚本,发现了一个here,这似乎工作得很好。

但是,当我点击“2”时,就像在第 2 页中一样,它只会在已经存在的那些下方显示第 2 页的记录。所以基本上,如果我点击第 214 页,它仍然会在一页上显示所有记录。

我对 PHP 不是很有经验,所以我无法弄清楚 paginator.class.php 出了什么问题,希望这里有人可以。

这是页面上应该进行分页的代码:

else {
$query = "SELECT COUNT(*) FROM products";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_fetch_row($result);
$pages = new Paginator;  
$pages->items_total = $num_rows[0];  
$pages->mid_range = 9;  
$pages->paginate();  

$query1 = "SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit";
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo '<div style="margin-bottom:10px;display:inline-block;background-color:#E3E3E3;width:190px;height:200px;"><a href="'.$_SERVER['PHP_SELF'].'?serial='.$row['serial'].'"><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row['picture']).'"></a><br><div align="center"><b>'.htmlspecialchars($row['name']).'</b><br><h6>&euro;'.htmlspecialchars($row['price']).'</h6></div></div>&nbsp;';
};
echo '&nbsp;';
echo '<br><br><div style="margin-left:330px;">';
echo $pages->display_pages();
echo '</div>';
}

paginator.class.php可以在我刚才提到的网站上找到。

【问题讨论】:

    标签: php pagination


    【解决方案1】:

    问题在于您的查询:

    "SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit"
    

    你需要确定 $pages->limit 的值是多少。在我看来,与其计算应该在每个页面上显示多少条记录(假设为 10 条),然后确定您在哪个页面上并设置 LIMIT 条件。

    应该设置成这样:

    LIMIT 30, 10
    

    这是第 4 页 - 它显示记录 30-40,而不是我怀疑它在做什么,这是

    LIMIT 40
    

    该行将仅显示最多 40 条记录,并且不会关闭窗口的下限。

    仅供参考,看看manual 中的 MySQL SELECT 语法。

    【讨论】:

    • 据我所知,它确实在第 99-101 行中做出了该声明: $this->low = ($this->current_page-1) * $this->items_per_page; $this->high = (isset($_GET['ipp']) == 'All') ? $this->items_total:($this->current_page * $this->items_per_page)-1; $this->limit = (isset($_GET['ipp']) == 'All') ? "":" 限制 $this->low,$this->items_per_page";或者这不是设置此语句的正确方法?
    • a) $pages-&gt;limit 和 b) $query 的值是多少?
    • 我认为是这样的: $this->limit = (isset($_GET['ipp']) == 'All') ? "":" 限制 $this->low,$this->items_per_page"; $query 是您在回答中提到的那一行..
    • SMH...echo 将值显示在屏幕上,然后是 exit()。您会看到它们的值一清二楚,无需猜测。
    • 我得到:LIMIT 0,8 和 SELECT COUNT(*) FROM products 但结果应该是 10,因为这是记录的总数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2021-08-20
    • 2015-09-23
    相关资源
    最近更新 更多