0

我正在开发一个 codeigniter 项目,该项目获取结果并将其添加到带有分页的页面中。

我遇到的问题是,

该页面显示所有页码,但第一页似乎总是空的。当我移动到第二页时,它会显示第一页结果以及第二页结果。即使我再次移动到第一页,它也是空的。

当我进一步移动时,所有结果似乎都是堆叠而不是分页。

所以第一页 0 结果。第二页显示 30 个,第三页显示 45 个结果。

下面是我的脚本

 <script type="text/javascript">
            $(document).ready(function(){
                function loading_show(){
                    $('#loading').html("<img src='<?php echo $url?>images/loading.gif'/>").fadeIn('fast');
                }
                function loading_hide(){
                    $('#loading').fadeOut('fast');
                }                
                function loadData(page){
                    loading_show();                    
                    $.ajax
                    ({
                        type: "POST",
                        url: "<?php echo base_url()?>index.php/controls/ajaxload",
                        data: "page="+page,
                        success: function(msg)
                        {
                            $("#container").ajaxComplete(function(event, request, settings)
                            {
                                loading_hide();
                                $("#container").html(msg);
                            });
                        }
                    });
                }
                loadData(1); 
                $('#container .pagination li.active').live('click',function(){
                    var page = $(this).attr('p');
                    loadData(page);

                });           
                $('#go_btn').live('click',function(){
                    var page = parseInt($('.goto').val());
                    var no_of_pages = parseInt($('.total').attr('a'));
                    if(page != 0 && page <= no_of_pages){
                        loadData(page);
                    }else{
                        alert('Enter Number '+no_of_pages);
                        $('.goto').val("").focus();
                        return false;
                    }

                });
            });
        </script>

php代码

function ajaxload()
{

if($_POST['page'])
{

    $page = $_POST['page'];
    $cur_page = $page;
    $page -= 1;
    $per_page = 15;
    $previous_btn = true;
    $next_btn = true;
    $first_btn = true;
    $last_btn = true;
    $start = $page * $per_page;
    $city ='London';


    $this->load->model('control_model');

    $query_pag_datas = $this->control_model->data_for_pagination($start, $per_page,$city);
    $msg = "";

    foreach ($query_pag_datas as $single):

    $htmlmsg = htmlentities($single['Description']);
    $msg .= "<li><b>" . $single['offID'] . "</b> " . $htmlmsg . "</li>";

    endforeach;

    $msg = "<div class='data'><ul>" . $msg . "</ul></div>"; 


    $count = $this->control_model->count_pages($city);
    $no_of_paginations = ceil($count / $per_page);

    if ($cur_page >= 7) {
        $start_loop = $cur_page - 3;
        if ($no_of_paginations > $cur_page + 3)
            $end_loop = $cur_page + 3;
        else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
            $start_loop = $no_of_paginations - 6;
            $end_loop = $no_of_paginations;
        } else {
            $end_loop = $no_of_paginations;
        }
    } else {
        $start_loop = 1;
        if ($no_of_paginations > 7)
            $end_loop = 7;
        else
            $end_loop = $no_of_paginations;
    }

    $msg .= "<div class='pagination'><ul>";


    if ($first_btn && $cur_page > 1) {
        $msg .= "<li p='1' class='active'>First</li>";
    } else if ($first_btn) {
        $msg .= "<li p='1' class='inactive'>First</li>";
    }


    if ($previous_btn && $cur_page > 1) {
        $pre = $cur_page - 1;
        $msg .= "<li p='$pre' class='active'>Previous</li>";
    } else if ($previous_btn) {
        $msg .= "<li class='inactive'>Previous</li>";
    }
    for ($i = $start_loop; $i <= $end_loop; $i++) {

        if ($cur_page == $i)
            $msg .= "<li p='$i' style='color:#fff;background-color:#006699;' class='active'>{$i}</li>";
        else
            $msg .= "<li p='$i' class='active'>{$i}</li>";
    }


    if ($next_btn && $cur_page < $no_of_paginations) {
        $nex = $cur_page + 1;
        $msg .= "<li p='$nex' class='active'>Next</li>";
    } else if ($next_btn) {
        $msg .= "<li class='inactive'>Next</li>";
    }


    if ($last_btn && $cur_page < $no_of_paginations) {
        $msg .= "<li p='$no_of_paginations' class='active'>Last</li>";
    } else if ($last_btn) {
        $msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>";
    }
    $goto = "<input type='text' class='goto' size='1' style='margin-top:-1px;margin-left:60px;'/><input type='button' id='go_btn' class='go_button' value='Go'/>";
    $total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>";
    $msg = $msg . "</ul>" . $goto . $total_string . "</div>";  
    echo $msg;
}

}

任何帮助将不胜感激。

4

2 回答 2

1

改变

$page -= 1;

$page = ($page > 0 ) ? $page -1 : 0 ;

在第一次点击页面是 0 所以

0-1 => -1 

-1 * 15 => -15 ; 

你的第一个偏移量是-15

于 2013-03-21T17:36:59.617 回答
1

因此,如果您发布的页面是1,这就是您得到的:

第 1 页:

$page = $_POST['page'];//let's say we're posting page 1, so $page === 1
...
$page -= 1;//so now, page === 0
...
$start = $page * $per_page;//which, since math, will be 0
...
$query_pag_datas = $this->control_model->data_for_pagination($start, $per_page,$city);
// you just called data_for_pagination(0, 15, 'London')

由于我们看不到该data_for_pagination方法,我将假设第一个参数是您请求的结果数。这是有道理的,因为随着页码的增加,结果的数量也会增加:

第2页:

$page = $_POST['page'];//let's say we're posting page 2, so $page === 2
...
$page -= 1;//so now, page === 1
...
$start = $page * $per_page;//which, since math, will be 15
...
$query_pag_datas = $this->control_model->data_for_pagination($start, $per_page,$city);
// you just called data_for_pagination(15, 15, 'London')

第 3 页:

$page = $_POST['page'];//let's say we're posting page 3, so $page === 3
...
$page -= 1;//so now, page === 2
...
$start = $page * $per_page;//which, since math, will be 30
...
$query_pag_datas = $this->control_model->data_for_pagination($start, $per_page,$city);
// you just called data_for_pagination(30, 15, 'London')

希望有帮助。

于 2013-03-21T18:21:00.440 回答