我正在尝试创建一个显示数据库中缩略图的页面。但是,如果我想更新数据库,我想创建一个分页。只有前3个表应在第1页中显示,依此类推...
这是我在缩略图中的脚本
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "test");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM news limit 3";
if($result = mysqli_query($link, $sql)){
if( mysqli_num_rows($result) > 0 ){
echo "<div class="container">";
echo "<table>";
echo "<tr>";
echo "</tr>";
while($row=mysqli_fetch_array($result)){
echo "<div class="col-md-4">";
echo "<div class="thumbnail">";
echo "<img alt="News" src="images/{$row["image"]}">";
echo "<div class="caption">";
echo "<b><h4>{$row["title"]}</b></h4>";
echo "<p>{$row["caption"]}</p>";
echo "<p align="right">";
echo "<a class="btn btn-primary" href="{$row["newsupdate"]}">Read More</a>";
echo "</p>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
<ul class="pagination">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
</ul>
您是否要嘲笑出现的表格?
我认为使用Bootstrap或jQuery的分页是一个不错的选择:
https://www.uno-de-piera.com/paginacion-original-con-jquery-y-php/https://esimakin.github.io/twbs-pagination/
您可以配置每页显示的项目,样式等。
为了通过您的记录集划分,将显示器限制为一次指定数量的记录,您需要更改SQL查询以使用offset
和total
值的limit
值条款。通常,分页使用获取变量来确定用户当前打开的页面,并且此变量在SQL查询中使用。
以下是我刚刚通过特定的记录集写的基本示例 - 使用代码并更改您的特定查询应该很简单。
<?php
$dbhost = 'localhost';
$dbuser = 'xxx';
$dbpwd = 'xxx';
$dbname = 'xxx';
$db = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
?>
<!doctype html>
<html>
<head>
<title>Basic Pagination</title>
<style>
#paging{border-top:1px solid black;margin:1rem auto;float:none;clear:both;}
#paging *{margin:0.5rem 1rem;clear:none!important;display:inline-block;}
.minimal{margin:0.5rem 0.1rem!important;}
</style>
</head>
<body>
<div id='results'>
<?php
/* Single user supplied parameter - the PAGE */
$page = isset( $_GET['page'] ) ? filter_input( INPUT_GET, 'page', FILTER_SANITIZE_NUMBER_INT ) : 0;
if( is_numeric( $page ) && $page >= 0 ){
$rpp = 10; /* Records Per Page */
$offset = $page * $rpp; /* paging offset */
/*
In order to determine the paging you need to know the total number of records available
that are returned by the query with any given WHERE clause. The sub-query therefore uses
the same WHERE clause and returns a number to be used later.
Edit the sql to suit your needs and edit the data that is rendered in the while loop.
*/
$sql="select
( select count(*) from `maps` where `location_enabled`=1 ) as 'count',
`id`,
`location_name` as 'name'
from `maps`
where `location_enabled`=1
limit {$offset},{$rpp}";
/*
Ignoring the possible SQL injection vulnerability - run the query
*/
$results=$db->query( $sql );
if( $results && $results->num_rows > 0 ){
/* Process the recordset however is appropriate for your use-case */
while( $rs=$results->fetch_object() ){
/* From sql query, the total number of records that satisfy the "WHERE" clause */
$count=$rs->count;
/* output data - thumbnails etc etc */
echo "<div>{$rs->id} - {$rs->name}</div>";
}
} else {
echo "Error! Unable to fetch results";
}
} else {
echo "Error... Bad foo!";
}
?>
</div>
<div id='paging'>
<?php
/*
Calculate links for basic pagination
( First,Previous,Next,Last )
*/
if( is_numeric( $page ) ){
$total_pages = floor( $count / $rpp );
$text='First';
if( $page == 0 ){
echo "<div>$text</div>";
} else {
echo "<a href='?page=0'>$text</a>";
}
$text='Previous';
if( $page > 0 ){
echo "<a href='?page=".max( 0, $page - 1 )."'>$text</a>";
} else {
echo "<div>$text</div>";
}
$text='Next';
if( $page >= $total_pages ){
echo "<div>$text</div>";
} else {
echo "<a href='?page=".min( $total_pages, $page + 1 )."'>$text</a>";
}
$text='Last';
if( $page==$total_pages ){
echo "<div>$text</div>";
} else {
echo "<a href='?page={$total_pages}'>$text</a>";
}
/* Generate basic hyperlink for each possible page */
for( $i=0; $i <= $total_pages; $i++ ) {
echo "<a class='minimal' href='?page={$i}'>".( $i + 1 )."</a>";
}
}
?>
</div>
</body>
</html>
<?php
$db->close();
?>