我正在使用codeigniter分頁。但是在使用codeingiter分頁時我遇到了一個問題。這是我面臨的問題。Codeigniter分頁不起作用
例如:如果頁面的記錄超過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");
}
}
請去這個網址: http://www.technicalkeeda.com/php-codeigniter/pagination-using-php-codeigniter –
@HardikPaghdar試過這個不能夠取數據只獲得分頁 – user7047368
信息缺失,以完全理解,...但:每頁應顯示多少項目? ...從你的控制器代碼看起來像你每頁顯示$ config ['per_page']項目。總是在您的代碼2!你應該這樣做: $ config ['per_page'] =($ this-> uri-> segment(N))? $ this-> uri-> segment(N):2; //其中N是指示每頁顯示項目的uri段。 – MarcM