【问题标题】:Pagination with search/filter using form使用表单进行搜索/过滤的分页
【发布时间】:2015-12-19 04:49:51
【问题描述】:

我是 codeigniter 框架的新手,我正在尝试使用搜索过滤器进行分页。

我遇到过诸如this(虽然没有答案)和this 之类的答案,也没有勾选答案,所以我不确定这是否是正确的遵循方式,而且很困惑。

我所拥有的是默认情况下页面将显示使用分页的表格的所有结果。

现在我陷入了困境并且陷入了混乱,如果有一些明显的错误,请原谅我。

所以我在这里要做的是,当用户在下拉框中选择一个值并提交时,假设客户 A,我希望仅来自 columncustomer 的行包含 Customer A

但是在提交后,我得到了所有结果,更糟糕的是,在没有我的页眉和页脚(单独的视图)的纯文本中。我明白那是因为我没有给他们打电话,但它仍然没有显示那些只有Customer A 的人。

一直在尝试找到一个简单的解决方案,在表单提交后,分页查询将根据从表单获取的值执行,并显示选定的行。除了这两个链接之外似乎找不到任何其他链接,所以我不确定我是否以正确的方式过滤。

查看

<form method="POST" action='<?php echo base_url("index.php/Home/load_lot_table")?>' class="form-inline">
     <select id="cust_drop_down" name="cust_drop_down" class="form-control input-mini">
         <option value="all">All</option>
         <option value="custA">Customer A</option>
         <option value="custB">Customer B</option>
     </select>

     <input type="submit" class="btn btn-primary purple_button" value="Search">
</form>

控制器

public function on_hold_lot() // Default function to display result 
{
    $data['title'] = 'Search System';

    $this->load->view('templates/normal_header', $data);
    $this->populate_customer_dropdown(); // private
    $this->load_lot_table(); // public
    $this->load->view('templates/legend_footer', $data);
}

public function load_lot_table() // Main pagination function
{

    if(isset($this->input->post))
    {
        $search = array(
        'customer' => $this->input->post('cust_drop_down')
        );
    }
    else
    {
        $search = array(
        'customer' => 'all',
        'stage' => 'all',
        'lot_status' => 'all'
        );
    }
    $config = array();
    $config['base_url'] = base_url()."index.php/Home/on_hold_lot";
    $config['total_rows'] = $this->home_model->record_count();
    $config['per_page'] = 10;
    $config['uri_segment'] = 3;
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Previous';

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

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $results = $this->home_model->fetch_lots($config['per_page'], $page, $search);

    $data['disp_rows'] = $results;
    $data['links'] = $this->pagination->create_links();

    return $this->load->view('home/lot_disposition', $data);
}

型号

public function record_count()
{
    return $this->db->count_all('disp_dummy');
}

public function fetch_lots($limit, $start, $search = null)
{
    $this->db->limit($limit, $start);

    if($search != null && $search['customer'] != 'all')
    {
        $this->db->where('customer', $search['customer']);
    }

    $query = $this->db->get('disp_dummy');

    if($query->num_rows() > 0)
    {
        foreach($query->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}

【问题讨论】:

    标签: php mysql codeigniter pagination codeigniter-pagination


    【解决方案1】:

    您需要对代码进行一些更改:

    首先使用 GET 请求提交您的搜索,因为您不会在第二页获得发布的参数进行分页:

    查看:

    <form method="GET" action='<?php echo base_url("index.php/Home/load_lot_table")?>' class="form-inline">
         <select id="cust_drop_down" name="cust_drop_down" class="form-control input-mini">
             <option value="all">All</option>
             <option value="custA">Customer A</option>
             <option value="custB">Customer B</option>
         </select>
    
         <input type="submit" class="btn btn-primary purple_button" value="Search">
    </form>
    

    在您的控制器中,您必须检查 get 值而不是 post 值。同样在$config['total_rows'] 中,您需要传递当前查询而不是表中存在的总行数。所以它会是这样的:

    控制器:

     public function load_lot_table() // Main pagination function
    {
    
        if($this->input->get('cust_drop_down'))
        {
            $search = array(
            'customer' => $this->input->get('cust_drop_down')
            );
        }
        else
        {
            $search = array(
            'customer' => 'all',
            'stage' => 'all',
            'lot_status' => 'all'
            );
        }
        $config = array();
        $config['base_url'] = base_url()."index.php/Home/on_hold_lot";
        $config['total_rows'] = $this->home_model->fetch_lots($search)->num_rows();
        $config['per_page'] = 10;
        $config['uri_segment'] = 3;
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
    
        $this->pagination->initialize($config);
    
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $results = $this->home_model->fetch_lots($search, $config['per_page'], $page)->result_array();
    
        $data['disp_rows'] = $results;
        $data['links'] = $this->pagination->create_links();
    
        return $this->load->view('home/lot_disposition', $data);
    }
    

    在您的模型中,在搜索功能中进行修改,如下所示:

    型号

    public function fetch_lots($search = null, $limit = null, $start = 0)
    {
       if($limit != null){
           $this->db->limit($limit, $start);
        }
    
    
        if($search != null && $search['customer'] != 'all')
        {
            $this->db->where('customer', $search['customer']);
        }
    
        $query = $this->db->get('disp_dummy');
    
        if($query->num_rows() > 0)
        {
           return $query;
        }
        else
        {
            return false;
        }
    }
    

    【讨论】:

    • 嗯,好吧,我按照你的建议进行操作,仍然可以显示,但我仍然以纯文本格式获取它,但带有过滤器。我觉得我return 的观点有问题。如何显示类似于我的on_hold_lot 函数的视图?
    • 哦,我只需要将表单操作更改为显示视图的功能。谢谢你的简单解释!
    • next 链接或第 2 页不起作用,但它将重置值并显示 all 的原始记录,但仅显示下一个 10 / 下一页。
    猜你喜欢
    • 2015-12-03
    • 2021-01-02
    • 1970-01-01
    • 2019-07-14
    • 2019-04-25
    • 2012-10-23
    • 2023-03-18
    • 2018-08-17
    • 2017-09-22
    相关资源
    最近更新 更多