2014-10-05 51 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 托比亞斯