簡體   English   中英

php分頁類和顯示問題

[英]Php Pagination class and display issue

我有一個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);

我有一個LIMIT為$ links-> limit()的mysql查詢,它正確顯示了10條記錄。

我稱分頁顯示為:

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

但是沒有顯示分頁,並且出現以下錯誤:

PHP注意:未定義的變量:......中的頁面,並在行上非對象上調用成員函數link()

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

我在同一文件夾中也上傳了pagination.css文件。

我的代碼有什么問題? 為什么在類方法中具有全局作用域的情況下,我為什么會注意到php是未定義的變量? 並在非對象上調用成員函數link()?

提前致謝。

注意:通過以下鏈接獲得了分頁類: 分頁類源

您已經創建了一個類定義,但是從未創建該類定義的實例。

您必須實例化課程?$page? 在PHP中以對象的形式獲取實例。 對於您的示例代碼,您必須將此對象實例保存(保存)在名稱為“ page”的變量中,該變量先前是使用global關鍵字定義的。

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

HTH Tobias

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM