使用 PHP 和 MySQL 的 Bootstrap 分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26984409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap Pagination using PHP & MySQL
提问by 8yt3c0d3
I am kind of new to Bootstrap, I am trying to implement pagination on one of the sections in my page to represent the data properly. Can anyone please help?
我是 Bootstrap 的新手,我试图在我的页面中的一个部分上实现分页以正确表示数据。有人可以帮忙吗?
Here is a snapshot of how the code looks right now. How can I implement pagination so that only 10 records are displayed? Thanks.
这是代码现在的样子的快照。如何实现分页以便只显示 10 条记录?谢谢。
<section class="success" id="all-confessions">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>All Confessions</h2>
<hr class="star-light">
</div>
</div>
<div class="row">
<div class="row text-left">
<?php
$allconfession = mysql_query("SELECT * FROM collection ORDER BY date DESC");
while($result2 = mysql_fetch_array($allconfession)) {
$id = $result2['id'];
?>
<div class="col-md-3 col-md-offset-1">
<h5>#<?php echo $id; ?></h5>
</div>
<div class="col-md-10 col-md-offset-1">
<p class="para-confess">
<?php
echo $result2['type'] ." from ". $result2['college'] ." of ". $result2['department'] ." confessed ". $result2['confession'];
?>
</p>
<div class="text-left">
<?php
if(isset($_COOKIE['uname'])) {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="cid" style="display: none;" value="<?php echo $id; ?>">
<button type="submit" class="btn btn-success fa fa-thumbs-up" name="like"> Cool</button>
<button type="submit" class="btn btn-warning fa fa-thumbs-down" name="dislike"> WTF</button>
</form>
<?php
}
?>
</div>
<div class="text-right">
<i class="fa fa-thumbs-o-up">
<?php
$likes = mysql_query("SELECT COUNT(*) FROM activity WHERE cid = $id AND ld = 1");
$alikes = mysql_fetch_row($likes);
echo $alikes[0];
?>
</i>
<i class="fa fa-thumbs-o-down">
<?php
$dislikes = mysql_query("SELECT COUNT(*) FROM activity WHERE cid = $id AND ld = 0");
$adislikes = mysql_fetch_row($dislikes);
echo $adislikes[0];
?>
</i>
</div>
<hr/>
</div>
<?php
}
?>
</div>
</div>
</div>
</section>
回答by CChoma
You're pretty far off.
你离得很远。
At the very minimum, you need to do the following.
至少,您需要执行以下操作。
- You need to create a variable that determines the number of items to show per page.
- You need to take that variable and multiply it by the page number to get the number of records to offset in your query.
- You need to offset the query results using the number you got from the calculation above and also limit the query to the number of items to show.
- You need to count the number of total items and divide it by the number of items per page to get the total number of pages to show in the pagination.
- 您需要创建一个变量来确定每页显示的项目数。
- 您需要获取该变量并将其乘以页码以获取要在查询中偏移的记录数。
- 您需要使用从上面的计算中获得的数字来抵消查询结果,并将查询限制为要显示的项目数。
- 您需要计算总项目数并将其除以每页的项目数以获得分页中显示的总页数。
Here is some simple pagination code that I've used many times in conjunction with Bootstrap.
这是一些简单的分页代码,我与 Bootstrap 结合使用了很多次。
http://www.a2zwebhelp.com/php-mysql-pagination
http://www.a2zwebhelp.com/php-mysql-pagination
Just omit the styles and apply Bootstrap classes to the elements. But you cannot just add static html and expect it to work without any kind of backend logic to it. Bootstrap only provides a way to style the pagination, but you have to build it.
只需省略样式并将 Bootstrap 类应用于元素即可。但是你不能只添加静态 html 并期望它在没有任何后端逻辑的情况下工作。Bootstrap 只提供了一种设置分页样式的方法,但您必须构建它。
回答by Wojciech Grzebieniowski
Firstly, please learn something about PDO http://php.net/manual/en/book.pdo.php. In my solution i assume you're using PDO.
首先,请了解 PDO http://php.net/manual/en/book.pdo.php。在我的解决方案中,我假设您使用的是 PDO。
First thing you need to do is determine how many rows there actually is in DB.
您需要做的第一件事是确定数据库中实际有多少行。
$nbOfResults = $pdo->query('select count(*) from collection')->fetchColumn();
Then set some limit of entities per page.
然后设置每页实体的一些限制。
$entitiesPerPage = 10;
Now let's determine how many pages there should be. First I'll divide number of results by entitiesPerPage. Let's say there're 202 results. Dividing it by 10 ( entities per page ) will result with 20 pages ( casted to int ). However there're still 2 entities left. That's why i have to check modulo and add one more page if needed.
现在让我们确定应该有多少页。首先,我将按entitiesPerPage 划分结果数。假设有 202 个结果。将其除以 10(每页实体)将产生 20 页(转换为 int)。但是仍然有 2 个实体。这就是为什么我必须检查模数并在需要时再添加一页的原因。
$nbOfPages = intval($nbOfResults / $entitiesPerPage);
if( ($entitiesPerPage % $nbOfResults) !== 0 ) {
$nbOfPages += 1
}
Now we're ready to build a pagination. But first we need to have a variable that holds current page.
现在我们准备建立一个分页。但首先我们需要有一个保存当前页面的变量。
$currentPage = $_GET['page'];
Or in more elegant way.
或者以更优雅的方式。
$currentPage = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_NUMBER_INT);
However, if there isn't any page let's assume we're on the first page.
但是,如果没有任何页面,让我们假设我们在第一页上。
if(!$currentPage) { $currentPage = 1 }
Ok, now it's time for an array holding pagination info.
好的,现在是保存分页信息的数组的时候了。
$pagination = [];
if ($currentPage !== 1) {
$pagination[] = [
'page' => 'Previous',
'link' => '?page=' . ($currentPage - 1),
'active' => false,
];
}
for($i = 1; $i <= $nbOfPages; $i++) {
$pagination[] = [
'page' => $i,
'link' => '?page=' . $i,
'active' => ( $i === $currentPage ),
];
}
if ($currentPage !== $nbOfPages) {
$pagination[] = [
'page' => 'Next',
'link' => '?page=' . ($currentPage + 1),
'active' => false,
];
}
And finally the query to get the results on the current Page.
最后是获取当前页面结果的查询。
$query = 'SELECT * FROM collection ORDER BY date DESC LIMIT ? OFFSET ?';
$sth = $dbh->prepare($query);
$sth->execute(array($entitiesPerPage, ( $currentPage - 1 ) * $entitiesPerPage)));
Now all you have to do is loop through the $pagination variable and print proper bootstrap HTML.
现在您要做的就是遍历 $pagination 变量并打印正确的引导程序 HTML。
回答by Felipe Rugai
I was facing the same situation, followed an article and adjusted it to my use. Here's what you need for your code.
我也面临着同样的情况,跟着一篇文章,根据我的使用情况进行了调整。这是您的代码所需的内容。
Tested with Bootstrap v3.3.5.
使用 Bootstrap v3.3.5 测试。
First of all: You should change your mysql_query()
function. This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. Read more here.
首先:你应该改变你的mysql_query()
功能。这个扩展在 PHP 5.5.0 中被弃用,在 PHP 7.0.0 中被移除。相反,应使用 MySQLi 或 PDO_MySQL 扩展。在这里阅读更多。
That being said, let's move to our PHP code.
话虽如此,让我们转向我们的 PHP 代码。
PHP(handles the MySQL queries and generates the pagination HTML)
Note: you can change your target page via $targetpage
variable.
PHP(处理 MySQL 查询并生成分页 HTML)
注意:您可以通过$targetpage
变量更改目标页面。
//PAGINATION//
$sql = mysqli_query("select * from collection");
$total = mysql_num_rows($sql);
$adjacents = 3;
$targetpage = "$_SERVER['PHP_SELF']"; //your file name
$limit = 10; //how many items to show per page
if(isset($_GET['page']))
{
$page = $_GET['page'];
}else{
$page = 0;
}
if($page){
$start = ($page - 1) * $limit; //first item to display on this page
}else{
$start = 0;
}
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is current page - 1
$next = $page + 1; //next page is current page + 1
$lastpage = ceil($total/$limit); //lastpage.
$lpm1 = $lastpage - 1; //last page minus 1
$sql2 = "SELECT * FROM collection";
$sql2 .= " order by date limit $start ,$limit ";
$sql_query = mysqli_query($sql2);
/* CREATE THE PAGINATION */
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<ul class='pagination'>";
if ($page > $counter+1) {
$pagination.= "<li><a href=\"$targetpage?page=$prev\"><</a></li>";
}
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<li><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
$pagination.= "<li>...</li>";
$pagination.= "<li><a href=\"$targetpage?page=$lpm1\">$lpm1</a></li>";
$pagination.= "<li><a href=\"$targetpage?page=$lastpage\">$lastpage</a></li>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<li><a href=\"$targetpage?page=1\">1</a></li>";
$pagination.= "<li><a href=\"$targetpage?page=2\">2</a></li>";
$pagination.= "<li>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
$pagination.= "<li>...</li>";
$pagination.= "<li><a href=\"$targetpage?page=$lpm1\">$lpm1</a></li>";
$pagination.= "<li><a href=\"$targetpage?page=$lastpage\">$lastpage</a></li>";
}
//close to end; only hide early pages
else
{
$pagination.= "<li><a href=\"$targetpage?page=1\">1</a></li>";
$pagination.= "<li><a href=\"$targetpage?page=2\">2</a></li>";
$pagination.= "<li>...</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage;
$counter++)
{
if ($counter == $page)
$pagination.= "<li><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<li><a href=\"$targetpage?page=$next\">></a></li>";
else
$pagination.= "";
$pagination.= "</ul>\n";
}
Now that we have the pagination set up, just call it wherever you want:
HTML
现在我们已经设置了分页,只需在任何你想要的地方调用它:
HTML
<div class="row">
<div class="col-md-12 text-center">
<?php echo $pagination ?>
</div>
</div>
Your <a>
elements will receive href=targetPage?page=pageNumber
您的<a>
元素将收到href=targetPage?page=pageNumber
SOURCE: http://www.a2zwebhelp.com/php-mysql-pagination
来源:http: //www.a2zwebhelp.com/php-mysql-pagination
Hope that helps!
希望有帮助!
回答by Ankit Verma
$page_no=$_POST['page_no'];//page number
$limit=$_POST['limit'];//number of data
$limit1 = $page_no*$limit; //calculate the limit
$start = $limit1-$limit; //calculate the start point
$sql = "select * from example limit $start,$limit";// query
use the code it will help
使用它会有所帮助的代码
回答by njwin07
I recently did something similar with bootstrap , i used responsive datatables to achieve this. here is the link here
我最近用 bootstrap 做了一些类似的事情,我使用响应式数据表来实现这一点。这里是链接在这里
things i got trouble with,
我遇到麻烦的事情,
- use latest api from google
- html , css and js files are provided by the site
- they provide pagination and responsivness all that you want
- the most important datatables accept data in array format , so when you echo the data from php use jason object and bind all the data in an array and then the rows you want to fill
- 使用谷歌最新的 api
- html , css 和 js 文件由网站提供
- 它们提供您想要的所有分页和响应性
- 最重要的数据表接受数组格式的数据,因此当您从 php 回显数据时,请使用 jason 对象并绑定数组中的所有数据,然后绑定要填充的行
回答by njwin07
For new users...
对于新用户...
$ppcompletexxkc = "yes";
$restt = $db->prepare('SELECT COUNT(*) FROM shop WHERE complete = :complete');
$restt->execute(array(':complete' => $ppcompletexxkc
));
$total = $restt->fetchColumn();
$adjacents = 3;
$targetpage = "category.php"; //your file name
$limit = 1; //how many items to show per page
$page = $_GET['page'];
if($page){
$start = ($page - 1) * $limit; //first item to display on this page
}else{
$start = 0;
}
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is current page - 1
$next = $page + 1; //next page is current page + 1
$lastpage = ceil($total/$limit); //lastpage.
$lpm1 = $lastpage - 1; //last page minus 1
$lksmttba = $db->prepare('SELECT * FROM shop WHERE complete = :complete ORDER BY id ASC LIMIT :start ,:limit ');
$lksmttba->execute(array(':complete' => $ppcompletexxkc,
':start' => $start,
':limit' => $limit
));
/* CREATE THE PAGINATION */
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class='pagination1'> <ul>";
if ($page > $counter+1) {
$pagination.= "<li><a href=\"$targetpage?page=$prev\">prev</a></li>";
}
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<li><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
$pagination.= "<li>...</li>";
$pagination.= "<li><a href=\"$targetpage?page=$lpm1\">$lpm1</a></li>";
$pagination.= "<li><a href=\"$targetpage?page=$lastpage\">$lastpage</a></li>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<li><a href=\"$targetpage?page=1\">1</a></li>";
$pagination.= "<li><a href=\"$targetpage?page=2\">2</a></li>";
$pagination.= "<li>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
$pagination.= "<li>...</li>";
$pagination.= "<li><a href=\"$targetpage?page=$lpm1\">$lpm1</a></li>";
$pagination.= "<li><a href=\"$targetpage?page=$lastpage\">$lastpage</a></li>";
}
//close to end; only hide early pages
else
{
$pagination.= "<li><a href=\"$targetpage?page=1\">1</a></li>";
$pagination.= "<li><a href=\"$targetpage?page=2\">2</a></li>";
$pagination.= "<li>...</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage;
$counter++)
{
if ($counter == $page)
$pagination.= "<li><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<li><a href=\"$targetpage?page=$next\">next</a></li>";
else
$pagination.= "";
$pagination.= "</ul></div>\n";
}
echo $pagination;
while($readpostv=$lksmttba->fetch(PDO::FETCH_ASSOC)){
echo' '.$readpostv['img1'].'';
}
echo $pagination;