Php 如何在codeigniter中创建分页

Php 如何在codeigniter中创建分页,php,codeigniter,pagination,Php,Codeigniter,Pagination,您好,我已经在PHP代码中实现了分页,但在单击分页链接时它不起作用。它显示所有页面的相同数据。下面是代码 控制器: class Testimonial extends CI_Controller { function __construct() { parent::__construct(); //here we will autoload the pagination library $this->load->library('pa

您好,我已经在PHP代码中实现了分页,但在单击分页链接时它不起作用。它显示所有页面的相同数据。下面是代码

控制器:

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

public function index()
{
    $this->load->model('testimonial_model');
    $config = array();
    $config["base_url"] = base_url('testimonial/index');
    $config['total_rows'] =   $this->db->count_all("testimonials");//here we will count all the data from the table
    $config['per_page'] = 6;//number of data to be shown on single page
    $config["uri_segment"] = 2;
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 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;
    }
}   
}
视图:


我不能发表评论,所以我只是回答一下

这里


这就是我在分页时使用的!还有很多!我也使用CI作为我的框架

我不能发表评论,所以我只是回答一下

这里


这就是我在分页时使用的!还有很多!我也使用CI作为我的框架

试试下面的方法可能会对你有所帮助

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

试试下面的方法可能会对你有所帮助

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


有谁能帮我把uri segmant 3:$page=($this->uri->segment(3))$此->uri->段(3):0@日进:谢谢你,工作顺利。。第1段=>推荐,第2段=>索引,第3段=>页面没有人能帮我用uri segmant3进行这个测试:$page=($this->uri->section(3))$此->uri->段(3):0@日进:谢谢你,工作顺利。。第1部分=>推荐,2=>index和3=>page nob但如何显示每页我只需要显示5,但如何显示每页我只需要显示5,在第一页我有6篇文章,在第二页我有2篇文章,一页应该显示6,如果我点击第二页,它显示的数据与第一页相同。打印你的查询,看看你会看到什么当你点击第二页时进入开始变量你尝试了上面的代码得到了与我相同的结果没有得到你说的内容你有没有尝试在点击第二页时打印你的查询?在第一页我有6篇文章,在第二页我有2篇文章,如果我点击第二页,它应该显示6篇与第一页中的数据相同。打印您的查询,并查看当您单击第二页时,在开始变量中会得到什么。尝试使用上述代码获得与我相同的结果没有得到您所说的内容单击第二页时,您是否尝试过打印您的查询?