Codeigniter (CI) Framework paging functions and related knowledge

Source: Internet
Author: User
This article mainly introduces the paging function of a self-encapsulated Codeigniter (CI) framework and the usage of the paging class of the Codeigniter (CI) framework. It is very simple and practical, and I hope it will be helpful to you.

This article mainly introduces the paging function of a self-encapsulated Codeigniter (CI) framework and the usage of the paging class of the Codeigniter (CI) framework. It is very simple and practical, and I hope it will be helpful to you.

Generally, data on the current page and total number of items need to be obtained when data is paged. The average person encapsulates two functions in the model to obtain the data on the current page and the total number of data items respectively. The business logic is similar, it feels redundant and can be encapsulated together:

The Code is as follows:


/**
* Retrieve paging data and the total number of items
* @ Param string @ tablename table name
* @ Param mixed $ where Condition
* @ Param int $ limit the number of entries per page
* @ Param int $ offset current page
*/
Public function get_page_data ($ tablename, $ where, $ limit, $ offset, $ order_by, $ db)
{
If (empty ($ tablename ))
{
Return FALSE;
}

$ Dbhandle = empty ($ db )? $ This-> db: $ db;

If ($ where)
{
If (is_array ($ where ))
{
$ Dbhandle-> where ($ where );
}
Else
{
$ Dbhandle-> where ($ where, NULL, false );
}
}

$ Db = clone ($ dbhandle );
$ Total = $ dbhandle-> count_all_results ($ tablename );

If ($ limit)
{
$ Db-> limit ($ limit );
}

If ($ offset)
{
$ Db-> offset ($ offset );
}

If ($ order_by)
{
$ Db-> order_by ($ order_by );
}

$ Data = $ db-> get ($ tablename)-> result_array ();

Return array ('Total' => $ total, 'data' => $ data );
}

CI framework paging usage experience

There are four methods for the CI paging url address.
A) locahost/news/page/2 2 indicates the second page
B) localhost/news/page/20 indicates that the page starts from 20th records, that is, the first record on the page, which is the first record in the database.
C) localhost/news? Per_page = 2 second page
D) localhost/news? Per_page = 20 same as B)

First, let's take a look at the CI paging parameters:

The Code is as follows:


$ Config ['base _ url'] = $ url;
/* Basic paging URL
If you want to use the link form of a and B, the url should be in the form of/news/page/
If the link is in the form of c and d, the url should be like/news?
*/
$ Config ['total _ rows '] = $ total; // The total number of records. This is nothing to say, that is, you get the total number of records from the database.
$ Config ['per _ page'] = $ pagesize; // number of entries per page. Well, that's nothing to say .. Set it by yourself. The default value is 10.
$ Config ['page _ query_string '] = TRUE;
/* Parameter passing form. If this parameter is set to true, & per_page = 3 is automatically added to your url. (This per_page is the default query character. You can use $ config ['query _ string_segment '] to set it yourself)
Therefore, the form in c and d is generally localhost/news? & Per_page = 2 is the same, with no impact. Get per_page or 3
*/
$ Config ['first _ link'] = 'homepage '; // the first page is displayed.
$ Config ['last _ link'] = 'last page'; // The last page is displayed.
$ Config ['Next _ link'] = 'Next page> '; // next page
$ Config ['prev _ link'] = '<Previous Page'; // display Previous Page
$ Config ['cur _ tag_open '] = ''; // The start style of the current page.
$ Config ['cur _ tag_close '] = '';
/* The ending style of the current page. You can try this by yourself.
For example, I want to make the pagination numeric style of the current page look better and the font is red. You can add css code to current.
*/
$ Config ['num _ links '] = 2; // The number of pages displayed before and after the current connection. This means that the current page is 5th pages, so you can see 3, 4, 5, 6, 7 pages.
$ Config ['uri _ segment '] = 4;
/* This is when you use a) and B) Link styles to determine the page number.
For example, the uri_segment of localhost/news/page/3 must be set to 3. Set localhost/news/title/page/3 to 4.
*/
$ Config ['use _ page_numbers '] = TRUE;
/* This is the difference between a) and B. If it is enabled, page indicates the number of pages. False indicates the number of records.
*/

At the beginning, there were a lot of such statements when I checked data on the Internet.

The Code is as follows:


$ This-> model-> get_news ($ config ['per _ page'], $ this-> uri-> segment (3 ));

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.