[英]Bootstrap Pagination using PHP & MySQL
我是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>
你离我很远。
至少,您需要执行以下操作。
这里有一些简单的分页代码,我和Bootstrap一起多次使用过。
http://www.a2zwebhelp.com/php-mysql-pagination
只需省略样式并将Bootstrap类应用于元素。 但是你不能只添加静态html并期望它在没有任何后端逻辑的情况下工作。 Bootstrap只提供了一种对分页进行样式化的方法,但您必须构建它。
$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
使用它会有用的代码
我遇到了同样的情况,跟着一篇文章并根据我的用途调整了它。 这是您的代码所需要的。
使用Bootstrap v3.3.5进行测试。
首先 :你应该改变你的mysql_query()
函数。 此扩展在PHP 5.5.0中已弃用,并且已在PHP 7.0.0中删除。 相反,应该使用MySQLi或PDO_MySQL扩展。 在这里阅读更多。
话虽如此,让我们转到我们的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
希望有所帮助!
首先,请了解一些关于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变量并打印正确的bootstrap HTML。
我最近做了一些与bootstrap类似的东西,我使用响应式数据表来实现这一点。 这里是链接在这里
我遇到麻烦的事情,
对于新用户......
$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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.