2

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

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

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

现在我被卡住了,陷入了相当混乱,所以如果有一些明显的错误,请原谅我。

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

然而,在提交后,我得到了所有结果,更糟糕的是,纯文本没有我的页眉和页脚(单独的视图)。我明白那是因为我没有打电话给他们,但它仍然没有显示那些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;
    }
}
4

1 回答 1

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;
    }
}
于 2015-09-22T09:51:34.550 回答