donglong1465 2017-10-09 11:39
浏览 35

Codeigniter - 分页不起作用

I'm having some problems with Codeigniter and the pagination. The problem is that I get all the results/posts on all pages.

I'm having some problems with Codeigniter and the pagination. The problem is that I get all the results/posts on all pages.

Here is my Controller Code:

public function __construct()
{
    parent::__construct();

    $this->load->model('SS_shilpi_model');
    $this->load->library('form_validation');
    $this->load->library('pagination');
}

public function category(){
    $cid = $this->input->post('cid');
    if(isset($_GET['cid']))
    {
        $cid = $_GET['cid'];
        $this->SS_shilpi_model->catr3($cid);
        $this->SS_shilpi_model->catr3_nr($cid);
    }

    $config = [
        'base_url' => base_url('category/?cid='. $cid .' '),
        'per_page' => 12,
        'total_rows' => $this->SS_shilpi_model->catr3_nr($cid),
    ];

    $config['full_tag_open'] = '<ul  style="overflow: hidden;" class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['num_tag_open'] = '<li class="page-item">';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['next_tag_open'] = '<li class="page-item">';
    $config['next_tagl_close'] = '</a></li>';
    $config['prev_tag_open'] = '<li class="page-item">';
    $config['prev_tagl_close'] = '</li>';
    $config['first_link'] = 'First <i class="fa fa-caret-left fa-2x push-left-5 pull-right green-text"></i>';
    $config['first_tag_open'] = '<li class="page-item disabled">';
    $config['first_tagl_close'] = '</li>';
    $config['last_link'] = '<i class="fa fa-caret-right fa-2x push-left-5 pull-right green-text"></i> Last';
    $config['last_tag_open'] = '<li class="page-item">';
    $config['last_tagl_close'] = '</a></li>';
    $config['attributes'] = array('class' => 'page-link');
    $this->pagination->initialize($config); // model function

    $data['catr3d'] = $this->SS_shilpi_model->catr3($cid, $config['per_page'], $this->uri->segment(3)); // list of Category Details 
    // end pagination

    $this->load->view('shilpi/category',$data);
}

My Model Code :

public function catr3($cid){
    $this->db->select('');
    $this->db->order_by('id', 'DESC');
    $this->db->where('category_id',$cid);
    $query = $this->db->get('all_posts');
    return $query->result();
}

public function catr3_nr($cid){
    $this->db->select('');
    $this->db->order_by('id', 'DESC');
    $this->db->where('category_id',$cid);
    $query = $this->db->get('all_posts');
    return $query->num_rows();

}

My view code :

<div id="catdata" class="col-xs-12 no-pull push-up-5 push-down-5 briefs">

              <?php
                   $count =1;
                    foreach($catr3d as $r)
                    {

                    if($count%4 == 1)
                    { 
                            echo '<div class="col-xs-12 no-pull push-down-5">';

                    }

                    ?>

                <div class="col-sm-3 col-xs-12 no-pull-left pull-right-5">
                    <div class="brief whitish-bg pull-5">
                        <p class="red-text no-push">
                            <?php echo $r->short_title; ?>
                        </p>
                        <a href="<?php echo base_url(); ?>details/?pid=<?php echo $r->id; ?>">
                            <h5 class="blue-text font17 push-up-5">
                                <?php echo $r->title; ?>
                            </h5>
                        </a>
                        <p>
                            <a href="<?php echo base_url(); ?>details/?pid=<?php echo $r->id; ?>">
                                <img src="<?php echo base_url(); ?>upload/images/<?php echo $r->photo;?>" height="75" width="100" class="pull-left push-right-5 push-up-5"></img>
                            </a>
                             <?php
                      $string = $r->news;
                        $string = strip_tags($string);

                        if (strlen($string) > 500) {


                            $pos=strpos($string, ' ', 900);
                            $stringCut=substr($string,0,$pos ); 
                            echo $stringCut;
                        }
                           else { 
                           echo $string;
                           }

                         ?>
                            <a href="<?php echo base_url(); ?>details/?pid=<?php echo $r->id; ?>"><i class="fa fa-caret-right fa-2x push-left-5 pull-right green-text"></i></a>
                        </p>
                    </div>
                </div>
                <?php 
                if ($count%4 == 0)
                    {
                        echo "</div>";
                    }
                    $count++;
                ?>
                <div class="clear: both"></div>
                <?php
                }
                if ($count%4 != 1) echo "</div>";
                ?>

                <?= $this->pagination->create_links() ?>

        </div>
  • 写回答

1条回答 默认 最新

  • dsifjgogw48491752 2017-10-09 12:20
    关注

    Add This to Controller start and limit to your model function argument

      $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
      $data['posts'] = $this->post->get_posts($config["per_page"], $page); 
    

    And following to model function get_posts

       $this->db->limit($limit, $start);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'