【发布时间】: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");
}
}
【问题讨论】:
-
@HardikPaghdar 尝试了这个无法获取仅分页的数据
-
信息缺失,无法完全理解,...但是:每页应该显示多少个项目? ...从您的控制器代码看来,您每页显示 $config['per_page'] 项。在您的代码中始终为 2!你应该这样做: $config['per_page'] = ($this->uri->segment(N)) ? $this->uri->segment(N) : 2; // 其中 N 是您指示要显示的每页项目的 uri 段。
-
标签: php codeigniter pagination