Codeigniter (CI) framework paging function and relevant knowledge _php example

Source: Internet
Author: User
Tags codeigniter

Generally in the data paging need to get the current page of data and total number of pages, the average person is in model packaging two functions to get the current page of data and data total number, business logic similar, feel a bit redundant, can be encapsulated together:

Copy Code code as follows:

/**
* Get the paging data and the total number of bars
* @param string @tablename table name
* @param mixed $where conditions
* @param int $limit number of pages 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 pagination Class usage experience

The URL address for CI pagination is four ways
A) LOCAHOST/NEWS/PAGE/2 This 2 represents the second page
b localhost/news/page/20 This 20 represents the beginning of the paging from the 20th record, the first record of the page, the 20th record in the database.
c) localhost/news?per_page=2 second page
D) localhost/news?per_page=20 and B)

First we look at the parameters of CI paging:

Copy Code code as follows:

$config [' base_url '] = $url;
/* The base URL for pagination
If you want to use a, B link form, the URL should be in the form of/news/page/
If the link is the form C, D, the URL should be like/news?
*/
$config [' total_rows '] = $total//Total number of records, this is nothing to say, you get the total number of records from the database
$config [' per_page '] = $pagesize; The number of bars per page. Well, there's nothing to say about this. Set yourself. The default is 10, like.
$config [' page_query_string '] = TRUE;
/* The form of the pass parameter. Opening true automatically adds &per_page=3 to your URL. (This per_page is the default query character, but you can also use $config[' query_string_segment ') to set it yourself)
So C, d in the form is generally for localhost/news?&per_page=2 but all the same, no impact. The per_page of Get is still 3
*/
$config [' first_link '] = ' home '; First page display
$config [' last_link '] = ' last '; Last page shows
$config [' next_link '] = ' next page > '; The next page shows
$config [' prev_link '] = ' < prev '; Previous page shows
$config [' cur_tag_open '] = ' <a class= ' current ' > '; Current page start style
$config [' cur_tag_close '] = ' </a> ';
/* Current page end style. You can try this on your own.
For example, I want to make the current page pagination digital style look a little, red font and so on. You can add CSS code to current
*/
$config [' num_links '] = 2;//The number of page numbers before and after the current connection. It means that your current page is page 5th, so you can see 3, 4, 5, 6, 7 pages.
$config [' uri_segment '] = 4;
/* This is when you use a, B) Link style, to determine the page number of pages.
For example LOCALHOST/NEWS/PAGE/3 this uri_segment will be set to 3. Localhost/news/title/page/3 This is going to be set to 4.
*/
$config [' use_page_numbers '] = TRUE;
/* This is the difference between a, b). is turned on, page will indicate the number of pages. False will indicate the number of records
*/

When I first started to look up information on the Internet, there were many of these.

Copy Code code as follows:

$this->model->get_news ($config [' per_page '], $this->uri->segment (3));

In fact, this type of writing is for B) this form of connection. The $this->uri->segment (3) Here is the number of records taken to PAGE/20 20. $config [' Per_page '] is the limit of how many output.
Has a lot of limitations and misleading. I started out dead and didn't know why it was written. Later found that the handbook is the best teacher.

After we have configured some of the parameters of the CI paging class, $this->pagination->initialize ($config);//Configure Paging

Copy Code code as follows:

$page = $this->pagination->create_links (); So we got the paging.

Pass directly to the view page.

As for how to load the model, how to access data records, how to transfer variables to the view, here is not to say, look at the manual.

Forgot to say, with the query parameters of the paging, I did so. View to submit the query parameter get to the controller's search method. In search, use $get = $this->input->get () to get the query parameters.
Then load model, with the query parameters and paging parameters to read the record, the results are displayed to the view ...

Also found a small bug, such as/news/page/-1000, the following pagination link will appear negative
Find system/libraries/pagination.php code as follows

Copy Code code as follows:

if ($this->use_page_numbers and $this->cur_page = = 0)
{
$this->cur_page = $base _page;
}
should be
if ($this->use_page_numbers and $this->cur_page <= 0)
{
$this->cur_page = $base _page;
}

That's right, after the change, the problem is gone.

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.