如何在 php 中使用引导程序分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23186133/
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
How to use bootstrap pagination with php
提问by Edwardo
I am a working with php and mysql and I managed to do pagination following this tutorial: http://www.phpfreaks.com/tutorial/basic-pagination
我正在使用 php 和 mysql,我设法按照本教程进行分页:http: //www.phpfreaks.com/tutorial/basic-pagination
Here is "mywebpage" (on work): http://ada.uprrp.edu/~ehazim/hpcf_proj/miejemplo.php
这是“我的网页”(工作中):http://ada.uprrp.edu/~ehazim/hpcf_proj/miejemplo.php
But Now, I want to make it "pretty" using bootstrap Pagination:http://getbootstrap.com/components/#pagination
但是现在,我想使用引导程序分页使其“漂亮”:http: //getbootstrap.com/components/#pagination
I am using _GET['current_page'] to get the page where I am. The problem is that I dont know how to change the echo's to echo the pagination from bootstrap... Yes, it may be stupid, but It is my first time with php and I am like 2 hours just trying to do this. Can someone help me? Below is the code that I have, following the tutorial of php freaks (which I understand, except for some echos with toomany quotes :/ ):
我正在使用 _GET['current_page'] 来获取我所在的页面。问题是我不知道如何更改回声以从引导程序回显分页...是的,这可能很愚蠢,但这是我第一次使用 php,我只是尝试了 2 个小时。有人能帮我吗?下面是我的代码,遵循 php freaks 教程(我理解,除了一些带有太多引号的回声:/):
<div class="pagination">
<ul>
<?php
/****** build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
# TRYING TO CHANGE HERE AND IN OTHER ECHOS
#echo "<li><a href=\"{$_SERVER['PHP_SELF']}?currentpage=1\">«</a></li>";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
#echo " <li><a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
# range of num links to show
$range = 3;
# loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
}
else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
# end build pagination links
?>
</ul>
</div>
采纳答案by David Taiaroa
To get you started, Bootstrap pagination works on a ul of class = pagination with the page links as list items.
为了让您开始,Bootstrap 分页在 class = pagination 的 ul 上工作,页面链接作为列表项。
Towards the start of your php code add the pagination class to the ul (not the div)
在你的 php 代码的开头将分页类添加到 ul(不是 div)
<ul class="pagination">
Then wherever you echo a page link, wrap it with li tags, e.g.
然后,无论您在何处回显页面链接,都用 li 标签包裹它,例如
echo " <li><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> </li>";
EDIT
You also want to tweak the HTML for the current page by changing
编辑
您还想通过更改来调整当前页面的 HTML
echo " [<b>$x</b>] ";
to
到
echo " <li>$x</li> ";
Edit
If you want to center your pagination bar, Bootstrap 3 has a the text-center class which you can use. See http://jsfiddle.net/panchroma/8RHzw/
Change the first line of your php to
编辑
如果您想将分页栏居中,Bootstrap 3 有一个您可以使用的 text-center 类。见http://jsfiddle.net/panchroma/8RHzw/
将你的php的第一行改为
<div class="text-center">
With Bootstrap 2, use
使用 Bootstrap 2,使用
<div style="text-align:center;">
Hope this helps!
希望这可以帮助!
回答by Dazza
Further to David Taiaroa's answer above, for the active button state you need to add the class="active" and A tags.
除了上面 David Taiaroa 的回答之外,对于活动按钮状态,您需要添加 class="active" 和 A 标签。
echo " <li>$x</li> ";
..becomes..
..变成..
echo " <li class=\"active\"><a>$x</a></li> ";