CodeIgniter framework to achieve the method of obtaining paging data and total number of _php examples

Source: Internet
Author: User
Tags codeigniter

In this paper, the CodeIgniter framework is used to obtain the paging data and the total number of bars. Share to everyone for your reference. The implementation methods are as follows:

Generally in the data paging need to get the current page data and total number of pages, the average person is in the model to encapsulate two functions to get the current page of data and data total bar number, business logic similar, feel a little redundant, can be packaged 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);
}

I hope this article will help you with the PHP program design based on 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.