分页和搜索显示每页上的所有记录

时间:2013-11-08 04:46:44

标签: codeigniter pagination

我正在使用codeigniter 2.1.4而我正在尝试在页面上显示所有用户文件,每页分页5个记录。分页链接有效,但每个链接都显示所有记录。

我的模特

public function get_user_files($user_id, $keyword = NULL) {
    $select =   array(
        'files.name as filename',
        'files.id, files.remarks',
        'boxes.id as box_id',
        'boxes.bin_id as bin_id',
        'box_types.name as box_type',
        'companies.name as companyname'
    );
    $from   =   array('files','boxes','box_types','companies');
    $where  =   array(
        'files.status'      =>  'Warehouse',
        'files.box_id'      =>  'boxes.id',
        'boxes.box_type_id' =>  'box_types.id',
        'boxes.company_id'  =>  'companies.id',
        'boxes.user_id'     =>  $user_id
    )

    $this->db
        ->select($select)
        ->from($from)
        ->where($where);

    if ($keyword) {
        $this->db->like('files.name', $keyword);
    }

    $query = $this->db->get()->result_array();
    return $query;
}

我的控制器

public function retrieve_box() {
    if ($this->correct_permission('client') == false) {
        redirect(base_url());
    }
    $this->load->helper('form');
    $keyword = $this->input->post('keyword');
    $data['files'] = $this->File->get_user_files($this->data['user_id'], $keyword);

    $total_rows = count($data['files']);

    // Pagination
    $this->load->library('pagination');

    $config['base_url'] = base_url() . 'client/service_request/type/retrieve-box';
    $config['uri_segment'] = 5;
    $config['total_rows'] = $total_rows;
    $config['per_page'] = 5;

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

    $data['pages'] = $this->pagination->create_links();

    $this->load->view('retrieve_box', $data);
}

我的网址是这样的

www.website.com/client/service_request/type/retrieve-box /

我的观点

<?php 
    $attributes = array('class' => 'standardForm', 'id' => 'retrieveBoxesForm');
    echo form_open('', $attributes);
    ?>
    <fieldset>
        <ul>
            <li>

                <?php
                    $data_form = array(
                        'name' => 'keyword',
                        'id' => 'boxes-field1',
                        'class' => 'textbox2',
                    );
                    echo form_input($data_form);
                ?>

                <?php
                    $data_form = array(
                        'class' => 'button2',
                        'value' => 'Search',
                    );
                    echo form_submit($data_form);
                ?>
            </li>
        </ul>
    </fieldset>

    <div class="clear"></div>
    <?php echo form_close(); ?>
    <div class="standardTable">
        <table>
            <thead>
                <tr>
                    <th><span class="checkboxReplacement"><input type="checkbox" /></span></th>
                    <th>Type</th>
                    <th>Filename</th>
                    <th>Box ID</th>
                    <th>Company</th>
                    <th>Location</th>
                    <th>Remarks</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>

                <?php foreach ($files as $file) { ?>
                    <tr>
                        <td><span class="checkboxReplacement"><input type="checkbox" /></span></td>
                        <td><?php echo $file['box_type']; ?></td>
                        <td><?php echo $file['filename']; ?></td>
                        <td><?php echo $file['box_id']; ?></td>
                        <td><?php echo $file['companyname']; ?></td>
                        <td><?php echo $file['bin_id']; ?></td>
                        <td><?php echo $file['remarks']; ?></td>
                        <td>
                            <a href="<?php echo base_url('client/retrievebox/' . $file['box_id']); ?>">Retrieve File</a> |
                            <a href="<?php echo base_url('client/retrievefile/' . $file['id']); ?>">Retrieve Box</a>
                        </td>
                    </tr>
                <?php } ?>

            </tbody>
        </table>
        <?php echo $pages; ?>
    </div>

目标 当用户访问上述链接时,会显示他/她的所有文件,但每页的分页数为5个。

他们可以搜索他们的文件名以获得更具体的记录。

我设法显示如下链接:

www.website.com/client/service_request/type/retrieve-box/
www.website.com/client/service_request/type/retrieve-box/5
www.website.com/client/service_request/type/retrieve-box/10

但是当我点击任何分页链接时,我会被带到相应的网址,但所有记录都会显示。

感谢您阅读本文。真的很难找到解决方案。

问候:)

2 个答案:

答案 0 :(得分:1)

$ data [&#39; pages&#39;] = $ this-&gt; pagination-&gt; create_links();

无需通过数据数组传递分页。(上面一行)

只需echo $this->pagination->create_links();

会做的。

另请更改$config['uri_segment']的值并检查。

<强>更新

好的。问题是你还没有为查询添加限制和偏移量。

添加到控制器

public function retrieve_box() {
if ($this->correct_permission('client') == false) {
    redirect(base_url());
}
$this->load->helper('form');
$keyword = $this->input->post('keyword');
$total = $this->File->get_user_files($this->data['user_id'], $keyword,'','');

$total_rows = count($total);

// Pagination
$this->load->library('pagination');

$config['base_url'] = base_url() . 'client/service_request/type/retrieve-box';
$config['uri_segment'] = 5;
$config['total_rows'] = $total_rows;
$config['per_page'] = 5;

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

$data['pages'] = $this->pagination->create_links();

$limit=$config['per_page'];
$offset= $this->uri->segment(5);

$data['files'] = $this->File->get_user_files($this->data['user_id'], $keyword,$limit,$offset);

$this->load->view('retrieve_box', $data);
}

<强>模型

public function get_user_files($user_id, $keyword = NULL,$limit='',$offset='') {
       if(!empty($limit)){
      $this->db->limit($limit);
      }
  //offset
    if(!empty($offset)){
     $this->db->offset($offset);
    }
    $this->db->select('files.name as filename, files.id, files.remarks')->from('files');
    $this->db->select('boxes.id as box_id, boxes.bin_id as bin_id')->from('boxes');
    $this->db->select('box_types.name as box_type')->from('box_types');
    $this->db->select('companies.name as companyname')->from('companies');
    $this->db->where('files.status', 'Warehouse');
    $this->db->where('files.box_id = boxes.id');
    $this->db->where('boxes.box_type_id = box_types.id');
    $this->db->where('boxes.company_id = companies.id');
    $this->db->where('boxes.user_id', $user_id);

    if ($keyword) {
        $this->db->like('files.name', $keyword);
    }

    $query = $this->db->get()->result_array();
    return $query;
}

答案 1 :(得分:1)

您需要two个功能。首先,计算用户拥有的文件总数,第二个是明智地获得结果分页。你在这里做错的是你在一个查询中获取所有记录并循环遍历所有结果。

 /* count all results */
function count_all_user_files($user_id, $keyword = NULL) {
    $this->db->from('files');
    $this->db->join('boxes', 'files.box_id = boxes.id');
    $this->db->join('box_types', 'boxes.box_type_id = box_types.id');
    $this->db->join('companies', 'boxes.company_id = companies.id');
    $this->db->where('files.status', 'Warehouse');
    $this->db->where('files.box_id = boxes.id');
    $this->db->where('boxes.box_type_id = box_types.id');
    $this->db->where('boxes.company_id = companies.id');
    $this->db->where('boxes.user_id', $user_id);
    if ($keyword) {
        $this->db->like('files.name', $keyword);
    }
    return $this->db->count_all_results();
}

/* get result to show */
function all_user_files( $user_id, $keyword = NULL, $limit, $offset ) {
    $this->db->select('files.name as filename, files.id, files.remarks');
    $this->db->select('boxes.id as box_id, boxes.bin_id as bin_id');
    $this->db->select('box_types.name as box_type');
    $this->db->select('companies.name as companyname');
    $this->db->from('files');
    $this->db->join('boxes', 'files.box_id = boxes.id');
    $this->db->join('box_types', 'boxes.box_type_id = box_types.id');
    $this->db->join('companies', 'boxes.company_id = companies.id');
    $this->db->where('files.status', 'Warehouse');
    $this->db->where('files.box_id = boxes.id');
    $this->db->where('boxes.box_type_id = box_types.id');
    $this->db->where('boxes.company_id = companies.id');
    $this->db->where('boxes.user_id', $user_id);
    if ($keyword) {
        $this->db->like('files.name', $keyword);
    }
    return $this->db->get()->result_array();
}

/* Config */
$config['base_url']     = base_url() . 'client/service_request/type/retrieve-box';
$config['uri_segment']  = 5;
$config['total_rows']   = $this->model_name->count_all_user_files( $user_id, $keyword );
$config['per_page']     = 5;

$data['files']          = $this->File->get_user_files($this->data['user_id'], $keyword, $config['per_page'], $this->uri->segment(5));