使用表单

时间:2015-09-22 09:12:43

标签: php mysql codeigniter pagination codeigniter-pagination

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

我遇到了诸如this(虽然没有回答)和this这些也没有回答答案的答案,所以我不确定它是否是'这是正确的方式,而且很困惑。

默认情况下,页面会显示使用分页的表格的所有结果。

现在我陷入困境并陷入一团糟,如果有一些明显的错误,请原谅我。

所以我在这里尝试做的是,当用户在下拉框中选择一个值并提交时,请说客户A,我期待包含Customer A的行仅来自customer列。

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

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

查看

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

1 个答案:

答案 0 :(得分: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>

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

CONTROLLER:

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