分页和搜索显示每页上的所有记录 [英] Pagination and search displaying all records on each page

查看:64
本文介绍了分页和搜索显示每页上的所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用codeigniter 2.1.4,并且试图在页面上显示所有用户文件,每页分页显示5条记录.分页链接有效,但是每个链接都显示所有记录.

I'm using codeigniter 2.1.4 and i'm trying to display all the users files on a page with pagination of 5 records per page. The pagination links work but every link shows all the records.

我的模特

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/

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条记录.

Objective When a user goes to the above link, all of his/her files are displayed but with pagination of 5 records per page.

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

They can search their file names to get a more specific record.

我设法像这样显示链接:

I've managed to display the links like so:

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

但是,当我单击任何分页链接时,我都被带到了相应的URL,但所有记录都在显示.

But when i click on any of the pagination links, i'm taken to the respective url but all the records are showing.

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

Thank you for reading this. Really struggling to find a solution.

此致:)

推荐答案

您需要two函数.首先,计算用户拥有的文件总数,其次,对结果进行分页.您在这里做错的是,您在单个查询中获取所有记录并遍历所有结果.

You need two functions for it. First, to count the total files a user has, and the second to get the results pagination wise. What you are doing wrong here is that you are fetching all the records in a single query and looping through all the results.

 /* 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));

这篇关于分页和搜索显示每页上的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆