【问题标题】:Codeigniter pagination is not workingCodeigniter 分页不起作用
【发布时间】:2016-12-05 10:19:08
【问题描述】:

我正在使用 codeigniter 分页。但是我在使用 codeingiter 分页时遇到了问题。这是我面临的问题。

例如:如果一个页面有超过 10 条记录,我在这里每页显示 5 条记录。如果我点击第二页,它会正确显示数据,但如果我需要返回第一页,它就不起作用。这是我的代码:

控制器:

class Testimonial extends CI_Controller {
function __construct() { 
        parent::__construct();
        //here we will autoload the pagination library
        $this->load->model('testimonial_model');
        $this->load->library('pagination');
    }
public function index()
{

    $config = array();
    $config["base_url"] = base_url('testimonial/index');
    $config['total_rows'] =   $this->testimonial_model->record_count();//here we will count all the data from the table
    $config['per_page'] = 2;//number of data to be shown on single page
    $config["uri_segment"] = 2;
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();//create the link for pagination
    $data['mainpage'] = "testimonial";
    $this->load->view('templates/template',$data);
}   
}

型号:

class Testimonial_model extends CI_Model
{    
function get_all_testimonials($limit, $start)
{
    $this->db->limit($limit, $start);
    $this->db->select('T.*');
    $this->db->from('testimonials AS T');
    $this->db->where(array('T.status'=>1));
    $q = $this->db->get();
    if($q->num_rows()>0)
    {
        return $q->result();
    }
    else 
    {
        return false;
    }
}

function record_count()
{
    return $this->db->count_all("testimonials");
}
}

【问题讨论】:

  • 请使用这个网址:technicalkeeda.com/php-codeigniter/…
  • @HardikPaghdar 尝试了这个无法获取仅分页的数据
  • 信息缺失,无法完全理解,...但是:每页应该显示多少个项目? ...从您的控制器代码看来,您每页显示 $config['per_page'] 项。在您的代码中始终为 2!你应该这样做: $config['per_page'] = ($this->uri->segment(N)) ? $this->uri->segment(N) : 2; // 其中 N 是您指示要显示的每页项目的 uri 段。

标签: php codeigniter pagination


【解决方案1】:

使用此答案请阅读代码一次了解它

public function index()
{

    $config = array();

    //$config["base_url"] = base_url('testimonial/index');

        $get_vars = $this->input->get();
        if(is_array($get_vars))
        $config['suffix'] = '?'.http_build_query($get_vars,'', "&");
        //$config['prefix'] = '?'.http_build_query($get_vars,'', "&");
        $config['base_url'] = base_url().$this->router->class.'/index';
        $config['first_url'] = $config['base_url'] . (isset($config['suffix'])?$config['suffix']:'');

    $config['total_rows'] =   $this->testimonial_model->record_count();//here we will count all the data from the table
    $config['per_page'] = 2;//number of data to be shown on single page


    //this line should be according to url to fetch actual value
    $config["uri_segment"] = ($this->uri->segment('2')?$this->uri->segment('2'):0); // 2 or 3 based on ur url


    $this->pagination->initialize($config);
    $page = $config["uri_segment"];
    $data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();//create the link for pagination
    $data['mainpage'] = "testimonial";
    $this->load->view('templates/template',$data);
}   

【讨论】:

  • 什么是页面?你能解释一下吗?它应该是 $page = $config["uri_segment"];
  • 找不到页面
  • 找不到页面的url是什么?请分享。
  • 如果我需要点击第 1 页,它再次为第 2 页工作,它处于禁用状态
  • $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;你用 $page= $config["uri_segment"] 改变了这个;并在 $this->uri->segment('2'); 中使用正确的数字2 或 3 根据您的需要
猜你喜欢
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
相关资源
最近更新 更多