在控制器中获取Scroll Pagination值

时间:2013-03-14 10:35:34

标签: php jquery codeigniter

我希望从jQuery到我的控制器获得 page_num 的值,但是我无法获得更新的值,我只获得了第一次传递的值。当我向下滚动并且它没有停止时我的滚动连续显示结果,并且它没有显示它刚刚显示的所有结果,然后连续重复它。

我正在使用jQuery ScrollPagination,但无法解决问题 - 请帮助我。

这是我的观点:

<div id="main" style="width:760px">
    <div id='friend_display'>
    <?php if($list->num_rows() > 0  ){
     foreach($list->result() as $show){     ?>
            <!-- image box -->
            <div class="image-box" style="margin-left:30px" id='image-holder' >

              <div class="photo-cover">
                <img width="160px" height="117px" src="<?=base_url()?>uploads/user_images/friends/<?php echo $show->user_image;?>" alt="" />
                    </div>

                    <p class="photo-name"><b><?php echo $show->user_name;?></b></p>
                    <!-- end photo name -->

            </div>
    <?php } } else { echo '<div align="center" style="color:#FF0000; font-size:17px; font-weight:bold">You have no Friends yet</div>';}?>
            <!-- end image-box -->
            <div class="cl">&nbsp;</div>
    </div>
    <!-- end main -->
    </div>

 <div class="loading1" id="loading1">Wait a moment... it's loading!</div>
 <div class="loading1" id="nomoreresults">Sorry, no more results....!</div>

这是我的控制者:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Friends extends CI_Controller {

    function __construct()
    {
            parent::__construct();
            $this->load->model('friends_model');
    }

    function find_friends($offset=0)
    {
            $friend['list']         =       $this->friends_model->find($offset);
            $this->load->view('friends_find_view',$friend);
    }


    function display_friends()
    {
            $offset =       $this->input->post('page_num');     
            $find   =       $this->friends_model->find($offset);

            if($find->num_rows() > 0        )
            {
                    foreach($find->result() as $show)
                    {       ?>

            <div class="image-box" style="margin-left:30px" id='image-holder' >
                    <div class="photo-cover">
                            <img width="160px" height="117px" src="<?=base_url()?>uploads/user_images/friends/<?php echo $show->user_image;?>" alt="" />
                    </div>

                    <p class="photo-name"><b><?php echo $show->user_name;?></b></p>
                    <!-- end photo name -->

            </div>
    <?php } } else { exit; }?>
            <!-- end image-box -->
            <div class="cl">&nbsp;</div>
            <?php          
    }
  }  

如果我回显$ offset 那么它每次在脚本中显示6 page_num 值正在改变corectly.t这是主要问题。

这是模型代码

         function find($offset)
         {
            $this->db->select('*');
            $this->db->from('users');

            $this->db->limit(6,$offset);
            $result = $this->db->get();
            return $result;        
          }

最后这是我的脚本

    <script type="text/javascript">
    var page_num = 1;
    $(function(){
    $('#friend_display').scrollPagination({
            'contentPage': '<?=base_url()?>friends/display_friends', // the url you are fetching the results
            'contentData': {page_num:$('.image-box').size()}, // these are the variables you can pass to the request, for example: children().size() to know which page you are
            'scrollTarget': $(window), // who gonna scroll? in this example, the full window
            'heightOffset': 10, // it gonna request when scroll is 10 pixels before the page ends
            'beforeLoad': function(){ // before load function, you can display a preloader div
                    $('#loading1').fadeIn();       
            },
            'afterLoad': function(elementsLoaded){ // after loading content, you can use this function to animate your new elements
                     $('#loading1').fadeOut();
                     $(elementsLoaded).fadeInWithDelay();
                     page_num:$('.image-box').size();
            }
    });

    // code for fade in element by element
    $.fn.fadeInWithDelay = function(){
            var delay = 0;
            return this.each(function(){
                    $(this).delay(delay).animate({opacity:1}, 200);
                    delay += 100;
            });
    };
});    

</script>

我第二次问这个问题,希望这次我会得到我的解决方案,谢谢。 您还可以在此处查看完整代码Code for scroll pagination

1 个答案:

答案 0 :(得分:1)

首先我要给你一个关于你的控制器的建议,你不应该将html与php混合

所以这段代码

function display_friends()
    {
            $offset =       $this->input->post('page_num');     
            $find   =       $this->friends_model->find($offset);

            if($find->num_rows() > 0        )
            {
                    foreach($find->result() as $show)
                    {       ?>

            <div class="image-box" style="margin-left:30px" id='image-holder' >
                    <div class="photo-cover">
                            <img width="160px" height="117px" src="<?=base_url()?>uploads/user_images/friends/<?php echo $show->user_image;?>" alt="" />
                    </div>

                    <p class="photo-name"><b><?php echo $show->user_name;?></b></p>
                    <!-- end photo name -->

            </div>
    <?php } } else { exit; }?>
            <!-- end image-box -->
            <div class="cl">&nbsp;</div>
            <?php          
    }

我们将它分成两部分,为此方法创建一个特定的视图:

function display_friends(){
    $offset = $this->input->get('page_num');//notice that we changed this to get     
    $find   = $this->friends_model->find($offset);

    if($find->num_rows() > 0)
    {
        $data['rows'] = $find->result();
        $this->load->view('new_view', $data);
    } 
    echo '<div class="cl">&nbsp;</div>'
}

new_view文件将是这样的

   <?php foreach($rows as $show):?>

    <div class="image-box" style="margin-left:30px" id='image-holder' >
    <div class="photo-cover">
        <img width="160px" height="117px" src="<?=base_url()?>uploads/user_images/friends/<?php echo $show->user_image;?>" alt="" />
    </div>
    <p class="photo-name"><b><?php echo $show->user_name;?></b></p>
    <!-- end photo name -->
    </div>
<?php endforeach;?>

修改 传递给插件的数据只是传递了一次,当它被实例化时,所以{page_num:$('.image-box').size()}实例化时为6,所以偏移量总是为6

'contentPage': '<?=base_url()?>friends/display_friends', // the url you are fetching the results
        'contentData': {page_num:$('.image-box').size()}, // these are the variables you can pass to the request, for example: children().size() to know which page you are
        'scrollTarget': $(window), // who gonna scroll? in this example, the full window
        'heightOffset': 10,

一种解决方案是编辑scrollpagination,并为发布数据或内部计数器创建一个setter。