简体   繁体   English

在codeigniter中进行手动分页

[英]make manual pagination in codeigniter

I make pagination function but the result is still incorrect. 我进行了分页功能,但结果仍然不正确。 The page I wanna show just 8 but the result showing till 9. So when I click number 9 shows nothing. 我要显示的页面仅显示8,但结果显示到9。因此,当我单击数字9时,什么也没有显示。 This is my pagination code: 这是我的分页代码:

<?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 } ?>        

I am not allowed to post on comments yet. 我还不允许发表评论。 I just wanna give you an idea. 我只是想给你一个主意。 Try to check your query first if it gives the right amount of data you wish to retrieve. 如果它提供了您希望检索的正确数量的数据,请尝试首先检查您的查询。

Here's one of my oldest pagination design, gladly I kept it. 这是我最古老的分页设计之一,很高兴我保留了它。

Here's the starting page: All working fine. 这是起始页:一切正常。 在此处输入图片说明

And here's the last page... as you can see, no result. 这是最后一页...如您所见,没有结果。 在此处输入图片说明

If your query is good, probably the problem is on your START & LIMIT. 如果您的查询很好,则可能是您的“开始和限制”出现了问题。

I used this code I saw from the internet years ago: (sorry, I deleted original comments) 我使用了几年前在互联网上看到的这段代码:(对不起,我删除了原始注释)

$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;?>

As a result, last page displays the right data 结果,最后一页显示正确的数据 在此处输入图片说明

I'm not good at CI, but I hope this helps. 我不擅长CI,但希望能有所帮助。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM