【问题标题】:PHP Pagination IssuesPHP分页问题
【发布时间】:2012-02-21 17:19:02
【问题描述】:

我似乎遇到了非常烦人的问题,我找到了一个出色的分页脚本,但它给我带来的只是麻烦,尽管设置时间很长,但我让它的工作标准正常,但是脚本没有似乎不明白我要控制“ipp”。

// PAGINATOR \\

    var $items_per_page;
    var $items_total;
    var $current_page;
    var $num_pages;
    var $mid_range;
    var $low;
    var $limit;
    var $return;
    var $default_ipp = 5;
    var $querystring;
    var $ipp_array;
    var $html;


    function Paginator()
    {
        $this->current_page = 1;
        $this->mid_range = 7;
        $this->ipp_array = array(10,25,50,100,'All');

    }

    function paginate($html)
    {
        if(!isset($_GET['ipp']))
        {
            $this->items_per_page = $this->default_ipp;
        } else {
        $this->items_per_page = $_GET['ipp'];
        }
        if($_GET['ipp'] == 'All')
        {
            $this->num_pages = 1;
            $this->items_per_page = $this->default_ipp;
        }
        else
        {
            if(!is_numeric($this->items_per_page) OR $this->items_per_page <= 0) $this->items_per_page = $this->default_ipp;
            $this->num_pages = ceil($this->items_total/$this->items_per_page);
        }
        if(!isset($_GET['page']) OR $_GET['page'] == '')
        {
            $this->current_page = 1; // must be numeric > 0
        } else {
        $this->current_page = (isset($_GET['page'])) ? (int) $_GET['page'] : 1 ; // must be numeric > 0
        }
        $prev_page = $this->current_page-1;
        $next_page = $this->current_page+1;
        if($_GET)
        {
            $args = explode("&",$_SERVER['QUERY_STRING']);
            foreach($args as $arg)
            {
                $keyval = explode("=",$arg);
                if($keyval[0] != "page" And $keyval[0] != "ipp") $this->querystring .= "&" . $arg . $html;
            }
        }

请注意,以上是完整编码的片段,你可以在这个地址找到完整的编码副本:http://net.tutsplus.com/tutorials/php/how-to-paginate-data-with-php

好的,所以我遇到的问题是我通过 _GET['ipp'] 传递给变量 ipp 的任何内容,它似乎完全忽略了,但是回显 _GET['ipp'] 效果很好..

例如 news.php?page=2&ipp=5&id=3。当我将 IPP 从 5 更改为另一个数字时,它会完全忽略我已经这样做并将 IPP(每页项目数)保持在 5 的事实吗?我现在对脚本感到非常困惑,这正是我正在寻找的,但似乎并没有按照我的方式工作。

感谢您提供任何潜在的帮助者,并感谢您的关注。

最好的问候

【问题讨论】:

  • 你有没有试过把这行注释掉看看是不是问题? if(!is_numeric($this-&gt;items_per_page) OR $this-&gt;items_per_page &lt;= 0) $this-&gt;items_per_page = $this-&gt;default_ipp;
  • 你好汉克,我确实注释掉了那行代码,它完全没有做任何事情,只是在脚本后面引起了问题!

标签: php pagination


【解决方案1】:

您意识到,如果您更改 $_GET['ipp],其他值可能不再相关,而且如果不运行代码,谁知道还会发生什么。

我的建议解决你的问题,一些基本的调试:

if($_GET) {
  // add some debugging here to see what all the calculated values are
  var_dump(array(
    $this->items_per_page,
    $this->items_total,
    $this->current_page,
    $this->num_pages,
    $this->mid_range,
    $this->low,
    $this->limit,
    $this->return,
    $this->default_ipp,
    $this->querystring,
    $this->ipp_array,
  ));

  // other code already in this block
  // ...
}

【讨论】:

  • 您好,感谢您的回复。这些结果的调试如下。 array(11) { [0]=&gt; string(1) "5" [1]=&gt; int(20) [2]=&gt; int(2) [3]=&gt; float(4) [4]=&gt; int(7) [5]=&gt; NULL [6]=&gt; NULL [7]=&gt; NULL [8]=&gt; int(5) [9]=&gt; string(14) "&amp;id=3#comments" [10]=&gt; NULL } :: 我使用的脚本可以在net.tutsplus.com/tutorials/php/how-to-paginate-data-with-php免费获得,如果你想亲自看看!
  • 我猜到一半了,但看起来这个类可能只允许根据这条虚假行$ipp_array = array(10,25,50,100,'All'); 为每页的项目设置一些预设值
  • 这似乎在一夜之间奇怪地解决了,我昨晚很晚才编辑代码,我似乎做了一些使它按预期工作的事情,我昨晚遇到了无法解决的错误除以零,因此如果没有设置 ipp,它应该使用默认值(为 5)。现在已解决,感谢所有帮助我的人,我不知道发生了什么!
猜你喜欢
  • 2010-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多