[英]make manual pagination in codeigniter
我进行了分页功能,但结果仍然不正确。 我要显示的页面仅显示8,但结果显示到9。因此,当我单击数字9时,什么也没有显示。 这是我的分页代码:
<?php if($current_page>1){ ?>
<nav class="pagination pagination-blue">
<a class="ajaxPage" href="<?=base_url()?>tracking/searchTiket?page=<?=($current_page-1)?>&&no_ticket=<?=$no_ticket?>&&s_month=<?=$month?>&&s_year=<?=$year?>">prev</a>
</nav>
<?php } ?>
<?php
$max = 5;
if($current_page < $max)
$sp = 1;
elseif ($current_page >= ($page_size - floor($max/2)) )
$sp = $page_size - $max + 1;
elseif($current_page >= $max)
$sp = $current_page - floor($max/2);
for($i = $sp; $i<=($sp + $max -1); $i++){
if($i > $page_size)
continue;
if($current_page == $i)
?>
<nav class="pagination pagination-blue">
<a class="ajaxPage" href="<?=base_url()?>tracking/searchTiket?page=<?=$i?>&&no_ticket=<?=$no_ticket?>&&s_month=<?=$month?>&&s_year=<?=$year?>"><?=floor($i)?></a></nav>
<?php
}
?>
<?php if(ceil($page_size)!=($current_page+1)){ ?>
<nav class="pagination pagination-blue">
<a class="ajaxPage" href="<?=base_url()?>tracking/searchTiket?page=<?=($current_page+1)?>&&no_ticket=<?=$no_ticket?>&&s_month=<?=$month?>&&s_year=<?=$year?>">next</a> </nav>
<?php } ?>
我还不允许发表评论。 我只是想给你一个主意。 如果它提供了您希望检索的正确数量的数据,请尝试首先检查您的查询。
这是我最古老的分页设计之一,很高兴我保留了它。
如果您的查询很好,则可能是您的“开始和限制”出现了问题。
我使用了几年前在互联网上看到的这段代码:(对不起,我删除了原始注释)
$tbl_name="your_table";
$adjacents = 10; // (10*2)+1 = 21 pages between next and prev
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysqli_fetch_array(mysqli_query($query));
$total_pages = $total_pages[num];
$targetpage = "same_page.php";
$limit = 100; // rows to be displayed per page
$page = $_GET['page'];
if($page)
{
$start = ($page - 1) * $limit; // ** this caused the problem **
$start = ($page + $limit) - 1; // ** this fixed the problem **
}
else{
$start = 0;
}
$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit ";
$result = mysqli_query($sql);
if ($page == 0) $page = 1;
$prev = $page - 1;
$Next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//Prev button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?search=$get&page=$prev\" >« Latest </a>";
else
$pagination.= "<span class=\"disabled\">« Latest </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?search=$get&page=$counter\" > $counter</a>"; // just ignore search=$get, I believe you don't need this
}
}
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?search=$get&page=$counter\" >$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?search=$get&page=$lpm1\" >$lpm1</a>";
$pagination.= "<a href=\"$targetpage?search=$get&page=$lastpage\" >$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?search=$get&page=1\" >1</a>";
$pagination.= "<a href=\"$targetpage?search=$get&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?search=$get&page=$counter\" >$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?search=$get&page=$lpm1\" >$lpm1</a>";
$pagination.= "<a href=\"$targetpage?search=$get&page=$lastpage\" >$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?search=$get&page=1\" >1</a>";
$pagination.= "<a href=\"$targetpage?search=$get&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?search=$get&page=$counter\" >$counter</a>";
}
}
}
//Next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?search=$get&page=$Next\" > Prev »</a>";
else
$pagination.= "<span class=\"disabled\"> Prev »</span>";
$pagination.= "</div>\n";
}
?>
/**
* YOUR TABLE
* GOES
* HERE
*/
<?php echo $pagination;?>
我不擅长CI,但希望能有所帮助。 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.