search
Homephp教程php手册Codeigniter框架实现获取分页数据和总条数的方法

这篇文章主要介绍了Codeigniter框架实现获取分页数据和总条数的方法,实现了对获取当前页的数据和总条数方法的封装,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了Codeigniter框架实现获取分页数据和总条数的方法。分享给大家供大家参考。具体实现方法如下:

一般在数据分页的时候需要获取当前页的数据和总条数,,一般人是在model中封装两个函数分别获取当前页的数据和数据总条数,业务逻辑类似,感觉有点冗余,可以封装在一起

复制代码 代码如下:

/**
* 获取分页数据及总条数
* @param string @tablename 表名
* @param mixed $where 条件
* @param int $limit 每页条数
* @param int $offset 当前页
*
*/
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);
}

希望本文所述对大家基于Codeigniter框架的PHP程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software