Codeigniter - 分页不起作用

时间:2017-10-09 11:39:29

标签: php codeigniter pagination

我在Codeigniter和分页方面遇到了一些问题。问题是我在所有页面上都获得了所有结果/帖子。

我在Codeigniter和分页方面遇到了一些问题。问题是我在所有页面上都获得了所有结果/帖子。

这是我的控制器代码:

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);
}

我的型号代码:

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();

}

我的观看代码:

<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 个答案:

答案 0 :(得分:0)

将此添加到控制器启动并限制到模型函数参数

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

关注模型函数get_posts

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