CodeIgniter pagination usage example, codeigniter example
This example describes how to use the pagination paging class of CodeIgniter. We will share this with you for your reference. The details are as follows:
Controller (application/controller/page. php file ):
Public function index () {$ this-> load-> model ('home _ model', '', TRUE); $ config = array (); $ config ['per _ page'] = $ this-> per_page; // The number of data displayed on each page $ current_page = intval ($ this-> input-> get_post ('per _ page', true )); // obtain the current page number. // restore the page if (0 = $ current_page) {$ current_page = 1;} $ offset = ($ current_page-1) * $ config ['per _ page']; // sets the offset to limit the start position of the Data Query (starting from $ offset) $ result = $ this-> home_model-> index ($ Fset, $ config ['per _ page'], $ order = 'id desc '); $ config ['base _ url'] = $ this-> config-> item ('base _ url '). 'admin/home/index? '; $ Config ['first _ link'] = $ this-> first_link; // homepage $ config ['prev _ link'] = $ this-> prev_link; // Previous Page $ config ['Next _ link'] = $ this-> next_link; // next page $ config ['last _ link'] = $ this-> last_link; // At the end of the page $ config ['total _ rows '] = $ result ['Total']; // The total number of items $ config ['num _ links'] = 3; // page number of connections $ config ['use _ page_numbers '] = TRUE; $ config ['page _ query_string'] = TRUE; $ this-> load-> library ('pagination'); // load the ci pagination class $ this-> pagination-> initialize ($ config ); $ result = array ('LIST' => $ result ['LIST'], 'Total' => $ result ['Total'], 'Current _ page' => $ current_page, 'per _ page' => $ config ['per _ page'], 'page' => $ this-> pagination-> create_links (),); $ this-> load-> view ('admin/home', $ result );}
Model (application/model/home_model.php file ):
public function index($offset,$num,$order='id desc'){ $query = $this->db->query( "SELECT Name_cn,Mall_type,create_time FROM smzdm_mall WHERE Is_deleted = 0 order by {$order} limit {$offset},{$num}"); return array( 'total' => $this->db->count_all('smzdm_mall',array('Is_deleted'=>'0')), 'list' => $query->result(), );}