2014-10-05 55 views
0

我有一个PHP分页类,代码如下:的PHP分页类和显示问题

<?php 

    class Pagination { 

     private $num_pages = 1; 
     private $start = 0; 
     private $display; 
     private $start_display; 

     function __construct ($query, $display=10) { 
     if (!empty($query)) { 
      $this->display = $display; 
      if (isset($_GET['display']) && is_numeric($_GET['display'])) $this->display = (int) $_GET['display']; 
      if (isset($_GET['np']) && is_numeric($_GET['np']) && $_GET['np'] > 0) { 
      $this->num_pages = (int) $_GET['np']; 
      } else { 
      if (is_numeric($query)) { 
       $num_records = $query; 
      } else { 
       $result = db_query ($query); 
       if ($result->num_rows > 1 || strstr($query, 'COUNT') === false) { 
       $num_records = $result->num_rows; 
       } else { 
       $row = $result->fetch_row(); 
       $num_records = $row[0]; 
       } 
      } 
      if ($num_records > $this->display) $this->num_pages = ceil ($num_records/$this->display); 
      } 
      if (isset($_GET['s']) && is_numeric($_GET['s']) && $_GET['s'] > 0) $this->start = (int) $_GET['s']; 
      $this->start_display = " LIMIT {$this->start}, {$this->display}"; 
     } 
     } 

     public function display ($split=5) { 
     global $page; 
     $html = ''; 
     if ($this->num_pages <= 1) return $html; 

     //$page->link('pagination.css'); 

     $url = $page->url ('add', '', 'np', $this->num_pages); 
     $current_page = ($this->start/$this->display) + 1; 
     $begin = $current_page - $split; 
     $end = $current_page + $split; 
     if ($begin < 1) { 
      $begin = 1; 
      $end = $split * 2; 
     } 
     if ($end > $this->num_pages) { 
      $end = $this->num_pages; 
      $begin = $end - ($split * 2); 
      $begin++; // add one so that we get double the split at the end 
      if ($begin < 1) $begin = 1; 
     } 
     if ($current_page != 1) { 
      $html .= '<a class="first" title="First" href="' . $page->url('add', $url, 's', 0) . '">&laquo;</a>'; 
      $html .= '<a class="prev" title="Previous" href="' . $page->url('add', $url, 's', $this->start - $this->display) . '">Previous</a>'; 
     } else { 
      $html .= '<span class="disabled first" title="First">&laquo;</span>'; 
      $html .= '<span class="disabled prev" title="Previous">Previous</span>'; 
     } 
     for ($i=$begin; $i<=$end; $i++) { 
      if ($i != $current_page) { 
      $html .= '<a title="' . $i . '" href="' . $page->url('add', $url, 's', ($this->display * ($i - 1))) . '">' . $i . '</a>'; 
      } else { 
      $html .= '<span class="current">' . $i . '</span>'; 
      } 
     } 
     if ($current_page != $this->num_pages) { 
      $html .= '<a class="next" title="Next" href="' . $page->url('add', $url, 's', $this->start + $this->display) . '">Next</a>'; 
      $last = ($this->num_pages * $this->display) - $this->display; 
      $html .= '<a class="last" title="Last" href="' . $page->url('add', $url, 's', $last) . '">&raquo;</a>'; 
     } else { 
      $html .= '<span class="disabled next" title="Next">Next</span>'; 
      $html .= '<span class="disabled last" title="Last">&raquo;</span>'; 
     } 
     return '<div class="pagination">' . $html . '</div>'; 
     } 

     public function limit() { 
     return $this->start_display; 
     } 

    } 

    ?> 

我调用类如下:

$page->link('pagination.css'); 
$links = new Pagination ($numrows); 

我有一个MySQL查询与极限$ links-> limit(),它正确显示了10条记录。

我打电话分页显示为:

$html .= $links->display(); 

但不显示页码,我收到以下错误:

PHP公告:未定义的变量:网页中...... 和 呼叫一个成员函数链路()的非对象上线

$page->link('pagination.css'); 

我在同一folde上传的文件pagination.css r太....

我的代码有什么问题?为什么我得到的通知,虽然有一个全局范围的类方法php是未定义的变量?并调用一个非对象的成员函数链接()?

在此先感谢。

注:从以下链接得到了分页类: Pagination Class Source

+0

您不必在页面类的链接()方法,而不必网页类/对象,其实。为了使用$ page var,你也必须使用这个类:http://php-ease.com/classes/page.html找到另一个分页类。 :) – sinisake 2014-10-05 06:51:39

+0

当前正在编写我自己的分页类。 – user3790186 2014-10-05 08:43:36

回答

0

您已经创建了一个类定义,但从来没有建立这个类定义的一个实例。

您必须在PHP中实例化类?$page?以获取其对象形式的实例。对于您的示例代码,您必须将此对象实例保存(保存)在名称为“page”的变量中,该变量以前用global关键字定义。

$html = ""; 
global $page; 
$page->link('pagination.css'); 
$links = new Pagination ($numrows); 
$html .= $page->display(); 
var_dump($html); 

HTH 托比亚斯