我需要帮助,使这个分页脚本稍微复杂一点。我需要将第61行的区域改为第80行,以考虑到这些行之间评论中描述的复杂性。
请看一看,看看你是否能想出一个简单的解决方案,并输出到$pages_content。。。
我已经提供了2个粘贴。。。
index.php:http://codepad.org/tWNvATES
和
数据库(MYSQL):http://codepad.org/cP8hGzWN
!!!——拜托,我只需要基本上替换第61行和第80行之间的内容,就可以输出类似的内容
"…4 5[6]7 8…"
作为$pages_content。。。
[编辑]
以下是代码片段:
$pages_content = '';
for($i = 1;$i <= $pages;$i++) {
if($pages >= 1) {
if($i == $page) {
$pages_content .= "<b>{$i}</b>";
}
else {
$pages_content .= "<a href='?page={$i}'>{$i}</a>";
}
}
}
在php中应用分页的最佳代码,请参阅
<?php
/*
Place code to connect to your DB here.
*/
include('config.php'); // include your code to connect to DB.
$tbl_name=""; //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
/* Setup vars for query. */
$targetpage = "filename.php"; //your file name (the name of this file)
$limit = 2; //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; //if no page var is given, set start to 0
/* Get data. */
$sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);
/* 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 page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class="pagination">";
//previous button
if ($page > 1)
$pagination.= "<a href="$targetpage?page=$prev">� previous</a>";
else
$pagination.= "<span class="disabled">� previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="$targetpage?page=$counter">$counter</a>";
}
}
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.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="$targetpage?page=$counter">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href="$targetpage?page=$lpm1">$lpm1</a>";
$pagination.= "<a href="$targetpage?page=$lastpage">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href="$targetpage?page=1">1</a>";
$pagination.= "<a href="$targetpage?page=2">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="$targetpage?page=$counter">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href="$targetpage?page=$lpm1">$lpm1</a>";
$pagination.= "<a href="$targetpage?page=$lastpage">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href="$targetpage?page=1">1</a>";
$pagination.= "<a href="$targetpage?page=2">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="$targetpage?page=$counter">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href="$targetpage?page=$next">next �</a>";
else
$pagination.= "<span class="disabled">next �</span>";
$pagination.= "</div>n";
}
?>
<?php
while($row = mysql_fetch_array($result))
{
// Your while loop here
}
?>
<?=$pagination?>
用于样式的css在这里:
div.pagination {
padding: 3px;
margin: 3px;
}
div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;
text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;
color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #000099;
font-weight: bold;
background-color: #000099;
color: #FFF;
}
div.pagination span.disabled {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #EEE;
color: #DDD;
}
希望它有帮助,快乐编码!
这样的东西应该能在中工作
$pages_content = '';
$minPage = max(1, $page - 2);
$maxPage = min($pages, $page + 2);
for($i = $minPage; $i <= $maxPage;$i++) {
if($pages >= 1) {
if($i == $page) {
$pages_content .= "<b>{$i}</b>";
}
else {
$pages_content .= "<a href='?page={$i}'>{$i}</a>";
}
}
}
如果页数可能很大,请考虑使用"对数"分页:
如何对许多页面进行页面导航?对数页面导航
(我的答案中包含了示例PHP代码)。
Amol Gholap 在PHP中的基本分页
public function getPagination($pid,$totalCount=500,$countPerPage=50,$pageNumberingStart=1,$url=BASE_PATH,$maxDisplayPageNumbers=10){
$response= "<a href='".($pid > 1 ? "$url?pid=".($pid-1) : "" )."' style='text-decoration:none'><-</a>";
$maxNum = $pid+($maxDisplayPageNumbers/2);
$minNum = $pid-($maxDisplayPageNumbers/2)+1;//-$pid);
$response.= $minNum > 1 ? "<a style='text-decoration:none'>---</a>":"";
for($t=0;$t<$totalCount;$t+=$countPerPage){
if(($maxNum >= $pageNumberingStart && $minNum <= $pageNumberingStart)
){
if($pid == $pageNumberingStart)
$response.= " <a href='$url?pid=$pageNumberingStart'><strong>".$pageNumberingStart."</strong></a> ";
else
$response.= " <a href='$url?pid=$pageNumberingStart' style='text-decoration:none'>".$pageNumberingStart."</a> ";
}
$pageNumberingStart++;
}
$response.= $maxNum < $pageNumberingStart-1 ? "<a style='text-decoration:none'>---</a>":"";
$response.= "<a href='".($pid < $pageNumberingStart ? "" : "$url?pid=".($pid+1) )."' style='text-decoration:none'>-></a>";
return $response;
}
echo getPagination($pageNumber=5,$totalCount=500,$countPerPage=50,$pageNumberingStart=1,$url=BASE_PATH,$maxDisplayPageNumbers=10);
获取当前页码
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
分页公式
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
获取的总页数
$total_pages_sql = "SELECT COUNT(*) FROM table";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
构造用于分页的SQL查询
$sql = "SELECT * FROM table LIMIT $offset, $no_of_records_per_page";
参考使用PHP和MySql 创建分页