簡體   English   中英

codeigniter分頁在一頁中顯示所有查詢結果

[英]codeigniter pagination showing all query results in one page

嗨,大家好,我正在嘗試使用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');
   }

控制器:

    $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
        }
    }

因此,必須從控制器調用search($ id,$ table,$ pageNumber)函數 常量也在config / constants.php文件中定義如下。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM