Codeigniter framework for obtaining paging data and total number of entries

Source: Internet
Author: User
Tags codeigniter
This article mainly introduces the Codeigniter framework to obtain the paging data and the total number of items. it is a very practical technique to encapsulate the methods for obtaining the data and total number of items on the current page, for more information, see the CI framework.

This example describes how to obtain the paging data and the total number of entries by using the Codeigniter framework. Share it with you for your reference. The specific implementation method is as follows:

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 );
}

I hope this article will help you design PHP programs based on the Codeigniter framework.

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.