在codeigniter中进行手动分页

时间:2017-08-03 06:47:51

标签: php codeigniter pagination

我制作分页功能但结果仍然不正确。我想要显示的页面只有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 } ?>        

1 个答案:

答案 0 :(得分:0)

我暂时不能发表评论。 我只想给你一个主意。 如果它提供了您想要检索的正确数据量,请先尝试检查您的查询。

这是我最古老的分页设计之一,很高兴我保留了它。

这里是起始页:一切正常。 enter image description here

这是最后一页......你可以看到,没有结果。 enter image description here

如果你的查询是好的,可能问题出在你的START&amp; LIMIT。

我使用了几年前从互联网上看到的代码:(抱歉,我删除了原始评论)

$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&nbsp</a>";
    else
        $pagination.= "<span class=\"disabled\">« Latest&nbsp</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\">&nbsp$counter</span>";
            else
                $pagination.= "<a href=\"$targetpage?search=$get&page=$counter\" >&nbsp$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\" >&nbspPrev »</a>";
    else
        $pagination.= "<span class=\"disabled\">&nbspPrev »</span>";
    $pagination.= "</div>\n";
}
?>

/**
*  YOUR TABLE 
*  GOES
*  HERE
*/ 

<?php echo $pagination;?>

因此,最后一页显示正确的数据 enter image description here

我不擅长CI,但我希望这会有所帮助。祝你好运!