【发布时间】:2015-07-31 20:07:14
【问题描述】:
我正在尝试进行分页,但我得到了
致命错误:在 C:\wamp\www\quotes\paginator.php 第 32 行对非对象调用成员函数 query()
我应该怎么做才能摆脱这个错误?我必须页面如下。 我阅读了给我的参考资料并从代码中获得了帮助,但我遇到了致命错误。如上所述。请查看我的代码并告诉我落后的地方。 我有两页。 index.php 和 paginator.php 为什么我会遇到我无法理解的致命错误。
index.php
<!DOCTYPE html>
<?php
require_once 'Paginator.php';
$con = new mysqli( 'localhost', 'root', 'pixster', 'quotes' );
$limit = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25;
$page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
$links = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;
$query = "SELECT table2.col2 AS a,table1.col2 AS b, table1.col1 AS c, table1.q_url AS d FROM table2, table1 WHERE table2.col1 = table1.col4 ";// AND table2.friendly_url= '".$authorname."'";
$Paginator = new Paginator( $con, $query );
$results = $Paginator->getData( $page, $limit );
?>
<?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?>
<tr>
<td><?php echo $results->data[$i]['$i']; ?></td>
<td><?php echo $results->data[$i]['$row['b']']; ?></td>
</tr>
<?php endfor; ?>
<html>
<head>
<title>quotes Pagination</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="col-md-10 col-md-offset-1">
<h1>PHP Pagination</h1>
<table class="table table-striped table-condensed table-bordered table- rounded">
<thead>
<tr>
<th width="20%">Sr. No</th>
<th width="20%">Quotes</th>
</tr>
</thead>
<tbody>
<?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?>
<tr>
<td><?php echo $results->data[$i]['$i']; ?></td>
<td><?php echo $results->data[$i]['$row['b']']; ?></td>
</tr>
<?php endfor; ?></tbody>
</table>
<?php echo $Paginator->createLinks( $links, 'pagination pagination-sm' ); ?>
</div>
</div>
</body>
</html>
分页器.php
<?php
class Paginator {
private $_con;
private $_limit;
private $_page;
private $_query;
private $_total;
public function _construct( $con, $query )
{
$this->_con = $con;
$this->_query = $query;
$rs= $this->_con->query( $this->_query );
$this->_total = $rs->num_rows;
}
public function getData( $limit = 10, $page = 1 ) {
$this->_limit = $limit;
$this->_page = $page;
if ( $this->_limit == 'all' ) {
$query = $this->_query;
} else {
$query = $this->_query . " LIMIT " . ( ( $this->_page - 1 ) * $this->_limit ) . ", $this->_limit";
}
$rs = $this->_conn->query( $query );
while ( $row = $rs->fetch_assoc() ) {
$results[] = $row;
}
$result = new stdClass();
$result->page = $this->_page;
$result->limit = $this->_limit;
$result->total = $this->_total;
$result->data = $results;
return $result;
}
public function createLinks( $links, $list_class ) {
if ( $this->_limit == 'all' ) {
return '';
}
$last = ceil( $this->_total / $this->_limit );
$start = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1;
$end = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last;
$html = '<ul class="' . $list_class . '">';
$class = ( $this->_page == 1 ) ? "disabled" : "";
$html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">«</a></li>';
if ( $start > 1 ) {
$html .= '<li><a href="?limit=' . $this->_limit . '&page=1">1</a> </li>';
$html .= '<li class="disabled"><span>...</span></li>';
}
for ( $i = $start ; $i <= $end; $i++ ) {
$class = ( $this->_page == $i ) ? "active" : "";
$html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a></li>';
}
if ( $end < $last ) {
$html .= '<li class="disabled"><span>...</span></li>';
$html .= '<li><a href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a></li>';
}
$class = ( $this->_page == $last ) ? "disabled" : "";
$html .= '<li class="' . $class . '"><a href="?limit=' . $this- >_limit . '&page=' . ( $this->_page + 1 ) . '">»</a></li>';
$html .= '</ul>';
return $html;
}
}
?>
【问题讨论】:
-
咳咳
$this->_conn-> -
我应该在哪里改变兄弟??
-
您已将其声明为
$this->_con = $con; -
您在执行查询时似乎从不检查连接问题或错误。
-
现在就开始回答吧。我已经说了要让你开始的话。
标签: php mysql pagination