php - codeigniter 分页在一页中显示所有查询结果

标签 php mysql codeigniter pagination

大家好,我正在尝试使用 codeigniter 分页显示 mysql 数据库记录..为此我编写了以下代码..当我运行此代码时,它会在一页中显示所有结果,同时页码显示正确根据给定的 per_page 限制..

型号:

  function get_records($user_name, $limit, $start) {
    $this->db->limit($limit, $start);
    $this->db->select('GROUP_CONCAT(cid) AS cIDs');
    $this->db->where('user_name', $user_name);
    $this->db->group_by("company_user");

    $this->db->from('company_records');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;
  }

  function count_records($user_name) {
    $this->db->select('GROUP_CONCAT(cid) AS cIDs');
    $this->db->where('user_name', $user_name);
    $this->db->group_by("company_user");
    return $this->db->count_all('company_records');
   }

Controller :

    $this->load->model('Records', 'Records', TRUE);
    $this->load->library('pagination');

    $config['base_url'] = base_url() . 'records/index';
    $config['total_rows'] = $this->Records->count_records($this->session->userdata('user'));
    $config['per_page'] = 10;
    $config["uri_segment"] = 4;
    $config['use_page_numbers'] = TRUE;

    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';

    $this->pagination->initialize($config);

    /* $start = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; */
    if ($this->uri->segment(4) > 0) {
        $offset = ($this->uri->segment(4) + 0) * $config['per_page'] - $config['per_page'];
    } else {
        $offset = $this->uri->segment(4);
    }
    $data['recordsDetails'] = $this->Records->get_records($this->session->userdata('user'), $config["per_page"], $offset);
    $data['pages'] = $this->pagination->create_links();

我已经尝试过谷歌搜索和stackoverflow..但尚未找到任何解决方案..可能是我的查询有问题或者我不知道..

请帮助我...

最佳答案

我使用此代码进行分页。

模型

    //Search for results
    public function search($id, $table, $pageNumber) {

        //Get the number of pages
        $numOfPage = $this->findResults($id, $table, RETURN_NUM_OF_PAGES, $pageNumber);

        if ($numOfPage < 1) {
            return false; //If there are no search results return false
        } else {
            $row = array();
            $res = $this->findResults($id, $table, RETURN_RESULTS, $pageNumber);
            for ($j = 0; $j < $res->num_rows(); $j++) {
                $row[$j] = $res->row($j);
            }
            return $row; //Return the results
        }
    }

    // Find the results from DB and return number of pages/ results
    function findResults($id, $table, $returnType, $pageNumber) {

        $this->db->where('id', $id);
        $this->db->order_by('reputation', 'desc'); //Order the users by reputation
        $itemsPerPage = 4;

        $startingItem = ($pageNumber - 1) * $itemsPerPage;

        if ($returnType == RETURN_RESULTS) {
            $res = $this->db->get($table, $itemsPerPage, $startingItem);
            return $res; //Return the results
        } else { //Return the number of pages
            $res = $this->db->get($table);
            $count = $res->num_rows(); //Get the total number of results

            $numofPages = (int) ($count / $itemsPerPage);
            if ($count % $itemsPerPage != 0)
                $numofPages = $numofPages + 1; //If the number of items < $itemsPerPage there should be 1 page
            return $numofPages; //Return the number of pages
        }
    }

因此,您必须从 Controller 调用函数search($id, $table, $pageNumber)。 常量也在 config/constants.php 文件中定义如下。

define('RETURN_NUM_OF_PAGES', 0);
define('RETURN_RESULTS', 1);

关于php - codeigniter 分页在一页中显示所有查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21310121/

相关文章:

php - 如何在 Heroku 上为 php 安装 gettext 扩展?

php - 关于 PHP 中的 OO 和类的几个问题

php - 使用数组构建 CSV

PHP这是header定位的最佳实践

php - 如何在 php 中用 for 计算逆数?

mysql - mysql表分组时检索最后一条记录

php - 获取 MySQL 查询结果作为其原生数据类型?

mysql - 仅日期搜索 DATETIME 或 DATE 格式哪个更快

php - Alamofire 和 Codeigniter RESTful 服务器

php - Codeigniter 中 $this->db->query_times 数组的时间格式是什么?