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.