我在{http://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928][1]遇到了这个php分页教程 我对此并不了解,因此很难实现它。
以下是进入Paginator.class.php文件的代码:
<?php
class Paginator {
private $_conn;
private $_limit;
private $_page;
private $_query;
private $_total;
public function __construct( $conn, $query ) {
$this->_conn = $conn;
$this->_query = $query;
$rs= $this->_conn->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;
}
}
?>
这是我放在index.php文件中的内容:
<?php
require_once 'Paginator.class.php';
$conn = new mysqli( 'host', 'user', 'password', 'database' );
$limit = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25;
$page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
$links = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;
$query = "select DATE_FORMAT(GDate,'%d/%m/%Y') as dd,name,comment,location from mytable where !GAllow=0 order by GDate desc";
$Paginator = new Paginator( $conn, $query );
$results = $Paginator->getData( $page, $limit );
?>
<!DOCTYPE html>
<head>
<title>PHP 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>Name</th>
<th width="20%">Message</th>
<th width="20%">City</th>
<th width="25%">Date</th>
</tr>
</thead>
<tbody>
<?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?>
<tr>
<td><?php echo $results->data[$i]['name']; ?></td>
<td><?php echo $results->data[$i]['comment']; ?></td>
<td><?php echo $results->data[$i]['location']; ?></td>
<td><?php echo $results->data[$i]['dd']; ?></td>
</tr>
<?php endfor; ?>
</tbody>
</table>
<?php echo $Paginator->createLinks( $links, 'pagination pagination-sm' ); ?>
</div>
</div>
</body>
</html>
当页面运行时,第25页,也只显示一行。单击下一页将向表中添加单行。怎么了?
答案 0 :(得分:0)
是。 __construct( $conn, $query )
,getData( $limit = 10, $page = 1 )
和createLinks( $links, $list_class )
是class Paginator
的方法。
结果,你的Paginator类最终会是这样的:
class Paginator {
private $_conn;
private $_limit;
private $_page;
private $_query;
private $_total;
public function __construct( $conn, $query ) { ... }
public function getData( $limit = 10, $page = 1 ) { ... }
public function createLinks( $links, $list_class ) { ... }
}