CodeIgniter (CI3.0) paging practice records recently learned B/S and selected the PHP CI framework as the starting point.
When I tried to create my personal CMS, I encountered the need to paging. the example of 3.0 or later versions could not be found on the internet. the code of the local experiment is attached below for your reference.
The database is as follows:
First, see Controller
Load-> model ('article _ model', 'article'); $ this-> load-> library ('pagination ');} /*** @ param int $ page can be seen as offset */public function index ($ page = 0) {// Three data items are displayed on each page. $ limit ['num'] = 3; $ limit ['offset'] = $ page; $ config ['base _ url'] = site_url ('P/index'); $ config ['total _ rows '] = $ this-> article-> get_articles_num (); // total number of data entries $ config ['per _ page'] = $ limit ['num']; // The number of lines displayed on each page $ this-> pagination-> initialize ($ config ); $ data = array ('Articles '=> $ this-> article-> get_limit_articles ($ limit); $ this-> load-> view ('page _ ex ', $ data );}}
Model again
Db-> from ('My _ article'); $ this-> db-> order_by ('posttime', 'desc '); $ query = $ this-> db-> get (); return $ query-> result_array ();} /*** get the number of data in the table * @ return mixed */public function get_articles_num () {return $ this-> db-> count_all ('My _ article ');} /*** get a limited number of data * @ param array $ arr * @ return mixed */public function get_limit_articles ($ arr = array ('num' => FALSE, 'offset' => FALSE) {if (isset ($ arr ['num']) and is Set ($ arr ['offset']) and ($ arr ['num']! = FALSE) and ($ arr ['offset']! = FALSE) {$ query = $ this-> db-> get ('My _ article', $ arr ['num'], $ arr ['offset']); return $ query-> result_array ();} else {return $ this-> get_all_articles ();}}}
View
pagination->create_links();
Attached running effect
Note that index/9 can be regarded as an index in the database, rather than a page number.