จาก CodeIniter User Guide : pagination class

วิธีการที่ง่ายที่สุดคือสร้าง query ไว้ใน control เรย(ไม่ต้องสร้าง model –เอาไว้ทดลองนะจ๊ะของจริงควรจะสร้าง model)

โดยต้องโหลด library ที่สำคัญมาใช้ก่อนคือ

  • $this->load->database();
  • $this->load->library(‘pagination’);

 ต่อไปสร้าง function page() ไว้สำหรับสร้าง pagination และข้อมูล

    function page($page=0)
{
        // config และเรียกใช้ pagination
$config['base_url'] = base_url().”Welcome/page/”;
$config['total_rows'] = $this->db->count_all(‘blocks’);
$config['per_page'] = ’5′;
$this->pagination->initialize($config);

        // query โดยใช้ get(table, result_per_page, offset) ซึ่งจะเท่ากัย SELECT * FROM table LIMIT offset, result_per_page
$query = $this->db->get(‘blocks’, $config['per_page'], $page);
$data['block']=$query->result_array();

        // โหลด views
$this->load->view(‘welcome_message’, $data);
}

 ทีนี้ก็เอา function นี้ไปใช้งานได้ตามสะดวก