我有一个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) . '">«</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">«</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) . '">»</a>';
} else {
$html .= '<span class="disabled next" title="Next">Next</span>';
$html .= '<span class="disabled last" title="Last">»</span>';
}
return '<div class="pagination">' . $html . '</div>';
}
public function limit () {
return $this->start_display;
}
}
?>
我给全班同学打电话如下:
$page->link('pagination.css');
$links = new Pagination ($numrows);
我有一个mysql查询,LIMIT为$links->LIMIT(),它正确显示了10条记录。
我将分页显示称为:
$html .= $links->display();
但是没有显示分页,我得到以下错误:
PHP注意:未定义的变量:page in。。。。。。和调用线上非对象上的成员函数link()
$page->link('pagination.css');
我也在同一个文件夹中上传了pagination.css文件。。。。
我的代码出了什么问题??为什么我注意到php是未定义的变量,尽管在类方法中有全局作用域?在非对象上调用成员函数link()??
提前谢谢。
注意:从以下链接获得分页类:分页类源
您已经创建了一个类定义,但从未创建过该类定义的实例。
你必须在PHP中实例化类?$page?
,才能获得一个对象形式的实例。对于你的示例代码,你必须将这个对象实例保存在一个名为"page"的变量中,这个变量以前是用全局关键字定义的。
$html = "";
global $page;
$page->link('pagination.css');
$links = new Pagination ($numrows);
$html .= $page->display();
var_dump($html);
HTHTobias