使用 PHP 和 MySQL 的引导分页
Posted
技术标签:
【中文标题】使用 PHP 和 MySQL 的引导分页【英文标题】:Bootstrap Pagination using PHP & MySQL 【发布时间】:2015-01-15 01:21:34 【问题描述】:我是 Bootstrap 的新手,我正在尝试在页面中的某个部分上实现分页以正确表示数据。有人可以帮忙吗?
这里是代码现在的样子的快照。如何实现分页以便只显示 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>
【问题讨论】:
你为什么不试试数据表库 【参考方案1】:你离得太远了。
至少,您需要执行以下操作。
-
您需要创建一个变量来确定每页显示的项目数。
您需要将该变量乘以页码,以获得查询中要偏移的记录数。
您需要使用从上面计算中获得的数字来抵消查询结果,并将查询限制为要显示的项目数。
您需要计算总项目数,然后将其除以每页的项目数,即可得出要在分页中显示的总页数。
这里有一些简单的分页代码,我已经多次与 Bootstrap 结合使用。
http://www.a2zwebhelp.com/php-mysql-pagination
只需省略样式并将 Bootstrap 类应用于元素。但是你不能只添加静态 html 并期望它在没有任何后端逻辑的情况下工作。 Bootstrap 只提供了一种设置分页样式的方法,但您必须自己构建它。
【讨论】:
补充说明(说实话很重要):不要使用 mysql_ 原型,因为它们已经过时了。 OP 直接解析查询中的值,这使得恶意用户很容易注入任何值并删除整个表。 @briosheje 是正确的。您应该使用更新的 mysqli 或 PDO 方法进行查询。除此之外,该脚本有助于理解如何构建分页。 我建议@ccoma 在答案中加入一些示例/工作代码。【参考方案2】: $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
使用有帮助的代码
【讨论】:
【参考方案3】:我最近用 bootstrap 做了一些类似的事情,我使用响应式数据表来实现这一点。这是链接here
我遇到的麻烦,
-
使用谷歌最新的 api
html、css、js文件由本站提供
它们提供您想要的所有分页和响应性
最重要的数据表接受数组格式的数据,因此当您从 php 回显数据时,请使用 jason 对象并将所有数据绑定到一个数组中,然后再绑定您要填充的行
【讨论】:
【参考方案4】:首先,请了解一下 PDO http://php.net/manual/en/book.pdo.php 。在我的解决方案中,我假设您正在使用 PDO。
您需要做的第一件事是确定 DB 中实际有多少行。
$nbOfResults = $pdo->query('select count(*) from collection')->fetchColumn();
然后设置每页实体的一些限制。
$entitiesPerPage = 10;
现在让我们确定应该有多少页。首先,我将结果数除以entitiesPerPage。假设有 202 个结果。除以 10(每页实体)将得到 20 页(转换为 int)。但是,还剩下 2 个实体。这就是为什么我必须检查模数并在需要时添加一页。
$nbOfPages = intval($nbOfResults / $entitiesPerPage);
if( ($entitiesPerPage % $nbOfResults) !== 0 )
$nbOfPages += 1
现在我们已准备好构建分页。但首先我们需要有一个保存当前页面的变量。
$currentPage = $_GET['page'];
或者以更优雅的方式。
$currentPage = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_NUMBER_INT);
但是,如果没有任何页面,我们假设我们在第一页。
if(!$currentPage) $currentPage = 1
好的,现在是保存分页信息的数组的时候了。
$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,
];
最后是在当前页面上获取结果的查询。
$query = 'SELECT * FROM collection ORDER BY date DESC LIMIT ? OFFSET ?';
$sth = $dbh->prepare($query);
$sth->execute(array($entitiesPerPage, ( $currentPage - 1 ) * $entitiesPerPage)));
现在您所要做的就是遍历 $pagination 变量并打印正确的引导 HTML。
【讨论】:
【参考方案5】:我也遇到了同样的情况,按照一篇文章进行了调整以适应我的使用。这是您的代码所需的内容。
使用 Bootstrap v3.3.5 测试。
首先:您应该更改mysql_query()
函数。此扩展在 PHP 5.5.0 中被弃用,并在 PHP 7.0.0 中被删除。相反,应该使用 MySQLi 或 PDO_MySQL 扩展。阅读更多here。
话虽如此,让我们转到 PHP 代码。
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";
现在我们已经设置了分页,只需在任何你想要的地方调用它:HTML
<div class="row">
<div class="col-md-12 text-center">
<?php echo $pagination ?>
</div>
</div>
您的<a>
元素将收到href=targetPage?page=pageNumber
来源:http://www.a2zwebhelp.com/php-mysql-pagination
希望有帮助!
【讨论】:
你应该在你的答案中提供足够的信息来直接回答这个问题。如果您链接到的页面消失了,那么您的回答将毫无用处。【参考方案6】:对于新用户...
$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;
【讨论】:
以上是关于使用 PHP 和 MySQL 的引导分页的主要内容,如果未能解决你的问题,请参考以下文章