【问题标题】:Php Pagination class and display issuePHP分页类和显示问题
【发布时间】:2014-10-05 06:24:43
【问题描述】:

我有一个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 是未定义的变量?并在非对象上调用成员函数链接()??

提前致谢。

注意:从以下链接获取分页类: Pagination Class Source

【问题讨论】:

    标签: php pagination


    【解决方案1】:

    您已创建类定义,但从未创建此类定义的实例。

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

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

    HTH 托比亚斯

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多