gpt4 book ai didi

php - 使用表单的搜索/过滤器分页

转载 作者:行者123 更新时间:2023-11-29 02:53:58 25 4
gpt4 key购买 nike

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

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

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

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

所以我想在这里做的是当用户在下拉框中选择一个值并提交让我们说客户 A,我期望行包含 Customer A仅限客户

但是在提交之后,我得到了所有的结果,更糟糕的是,在没有页眉和页脚的纯文本中(单独的 View )。我知道那是因为我没有给他们打电话,但它仍然不显示只有 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>

Controller

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

最佳答案

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

首先使用 GET 请求提交您的搜索,因为您不会在第二页中获得用于分页的张贴参数:

View :

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

在您的 Controller 中,您必须检查获取值而不是发布值。同样在 $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;
}
}

关于php - 使用表单的搜索/过滤器分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32712977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com