按照本指南,如何使用PHP对MySQL的结果进行分页:
https://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928
为paginator类文件结束:
<?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;
}
}
?>
它正在对结果进行分页,但我最终得到第1页的1个结果,第2页的2个结果,第3页的3个结果......等等。
任何人都可以帮我理解我在这里缺少的东西吗?
我的index.php文件:
<?php
require_once 'Paginator.class.php';
$conn = new mysqli( 'localhost', 'uname', 'password', 'dbname' );
$limit = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25;
$page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
$links = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;
$query = "SELECT Host, CVE, Name, Risk, CVSS FROM Vulnerabilities";
$Paginator = new Paginator( $conn, $query );
$results = $Paginator->getData( $page, $limit );
?>
<!DOCTYPE html>
<head>
<title>Vulnerability 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>Vulnerability Pagination</h1>
<?php echo $Paginator->createLinks( $links, 'pagination pagination-sm' ); ?>
<table class="table table-striped table-condensed table-bordered table-rounded">
<thead>
<tr>
<th width="15%">Host</th>
<th width="15%">CVE</th>
<th width="50%">Name</th>
<th width="10%">Risk</th>
<th width="10%">CVSS</th>
</tr>
</thead>
<tbody>
<?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?>
<tr>
<td><?php echo $results->data[$i]['Host']; ?></td>
<td><?php echo $results->data[$i]['CVE']; ?></td>
<td><?php echo $results->data[$i]['Name']; ?></td>
<td><?php echo $results->data[$i]['Risk']; ?></td>
<td><?php echo $results->data[$i]['CVSS']; ?></td>
</tr>
<?php endfor; ?>
</tbody>
</table>
<?php echo $Paginator->createLinks( $links, 'pagination pagination-sm' ); ?>
</div>
</div>
</body>
</html>
对我来说,它似乎是在迭代页码和每页的结果数量,但我无法确定原因..非常感谢任何帮助。