CodeIgniter (CI) framework paging function and related knowledge _php example

Source: Internet
Author: User
Tags codeigniter
Generally in the data paging time need to get the current page of data and total bar number, the average person is in the model encapsulated two functions to get the current page data and data total number of bars, business logic similar, feel a bit redundant, can be encapsulated together:

The

copy Code code is as follows:
/**
* Get paging data and total number of bars
* @param string @tablename table name
* @param mixed $where condition * @param int $limit number of bars per page
* @param int $offset Current page
*/
Public Function Get_page_data ($tablename, $where, $lim It, $offset, er _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 (er _by)
{
$db->order_by (er _by);
}

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

Return Array (' total ' = $total, ' data ' = $data);
}

CI Framework page-like use of the experience

There are four ways to address the URL of CI paging
A) LOCAHOST/NEWS/PAGE/2 This 2 represents the second page
b) Localhost/news/page/20 This 20 means that starting with the 20th record, paging, the first record of the page, is the 20th record in the database.
c) localhost/news?per_page=2 second page
D) localhost/news?per_page=20 with B)

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

Copy CodeThe code is as follows:
$config [' base_url '] = $url;
/* Base URL of the paging
If you want to use a, B link form, then the URL should be in the form of/news/page/
If the link is in the form C, D, the URL should be like/news?
*/
$config [' total_rows '] = $total;//The total number of records, this is nothing to say, you get the total number of records from the database
$config [' per_page '] = $pagesize; Number of bars per page. Well, there's nothing to say about this. Set yourself. The default is 10 as if.
$config [' page_query_string '] = TRUE;
/* Pass the Parameter form. Turn on true to automatically add &per_page=3 after your URL. (This per_page is the default query character, of course you can also use $config[' query_string_segment ') to set itself)
So the form in C and D is generally for localhost/news?&per_page=2, but all the same, no effect. The per_page of Get is still 3.
*/
$config [' first_link '] = ' home '; The first page shows
$config [' last_link '] = ' last '; The last page shows
$config [' next_link '] = ' next page > '; The next page shows
$config [' prev_link '] = ' < prev '; Previous page Show
$config [' cur_tag_open '] = '; Current page start style
$config [' cur_tag_close '] = ';
/* The current page end style. You can try this on your own.
For example, I want to make the current page of the page number style look good, red font and so on. You can add CSS code to the current.
*/
$config [' num_links '] = 2;//shows the number of page numbers before and after the current connection. This 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, used to determine page pages.
For example, LOCALHOST/NEWS/PAGE/3 this uri_segment will be set to 3. LOCALHOST/NEWS/TITLE/PAGE/3 this should be set to 4.
*/
$config [' use_page_numbers '] = TRUE;
/* This is a), b) difference. is turned on, the page will indicate the number of pages. False will indicate the number of records
*/

At the beginning of the online search for information, there are many such a notation.

Copy the Code code as follows:
$this->model->get_news ($config [' per_page '], $this->uri->segment (3));

In fact, this kind of writing is for B) this connection form. The $this->uri->segment (3) Here is 20 of the number of records taken in the PAGE/20. $config [' Per_page '] is the limit of the number of outputs.
Have a lot of limitations and misleading. I didn't even know why I was writing. Later found that the handbook is the best teacher.

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

Copy the Code code as follows:
$page = $this->pagination->create_links (); We're going to get a paging.

Pass directly to the view page.

As for how to load the model, how to access the data record, how to pass the variable to the view, here do not say, look at the manual is OK.

Forgot to say, with the query parameters of the paging, I did so. View submits the query parameter get to the controller's search method. In search, use $get = $this->input->get () to get to the query parameters.
The model is then loaded with query parameters and paging parameters to read the records and display the results to the view.

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

Copy the Code code as follows:
if ($this->use_page_numbers and $this->cur_page = = 0)
{
$this->cur_page = $base _page;
}
should be for
if ($this->use_page_numbers and $this->cur_page <= 0)
{
$this->cur_page = $base _page;
}

Well, after the modification, 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.