CodeIgniter (CI3.0) paging practices

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.