-
cyjuice
Newbie
-
Posts: 5
Threads: 3
Joined: Sep 2018
Reputation:
0
I have input type = "date" that allows user to pick a date range to run the query and show in table. But however, when I click Page 2 from the pagination , the date value lost, and the query does not search with the dates picked. How should I do to keep the value for query to run WHERE clause.
PHP Code: public function searchDate() {
$orders=new ReportModel;
$config["base_url"] = base_url() . "/Report/searchDate"; $config["total_rows"] = $orders->record_count_search(); $config["per_page"] = 150; $config["uri_segment"] = 3; $config["num_links"] = 10; $config['use_page_numbers'] = TRUE; $config['reuse_query_string'] = TRUE;
$this->pagination->initialize($config); $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['data']=$orders->get_orders_search($config["per_page"], $page); $data["links"] = $this->pagination->create_links();
$this->load->view('includes/header'); $this->load->view('estoreReport/list',$data); $this->load->view('includes/footer'); }
My View:
Code: <form class="form-inline" role="search" action="<?php echo base_url('estoreReport/searchDate')?>" method = "post">
<div class="form-group">
<input type="date" class="form-control" placeholder="Order Date From" name = "searchDateFrom" value="<?php echo set_value('searchDateFrom') ?>" >
<input type="date" class="form-control" placeholder="Order Date To" name = "searchDateTo" max="<?php echo date('Y-m-d'); ?>" value="<?php echo set_value('searchDateTo') ?>" >
</div>
<button class="btn btn-default " type="submit" value = "search"><i class="glyphicon glyphicon-search"></i>
</form>
Please help, thanks!
-
php_rocs
Administrator
-
Posts: 1,393
Threads: 80
Joined: Jun 2016
Reputation:
72
-
Wouter60
Posting Freak
-
Posts: 851
Threads: 38
Joined: Feb 2015
Reputation:
77
Add this to your controller function (at the top):
PHP Code: $datefrom = $this->input->post('searchDateFrom'); $dateto = $this->input->post('searchDateTo');
$config["base_url"] = base_url() . "/Report/searchDate"; $config["total_rows"] = $orders->record_count_search($datefrom,$dateto); // and a few lines further down: $data['data']=$orders->get_orders_search($config["per_page"], $page,$datefrom,$dateto);
Next, modify your model functions and make them accept the extra arguments $datefrom and $dateto.
PHP Code: public function record_count_search($datefrom,$dateto) { $this->db->select('*')->from('orders'); if ($datefrom AND $dateto) { $this->db->where('date >=' , $datefrom); $this->db->where('date <=' , $dateto); } return $this->db->count_all_results(); }
public function get_orders_search($limit, $offset, $datefrom, $dateto) { // similar as the other function, but returns array of records. }
-
cyjuice
Newbie
-
Posts: 5
Threads: 3
Joined: Sep 2018
Reputation:
0
(09-18-2018, 05:12 AM)php_rocs Wrote: @cyjuice,
All you have to do is determine if there are values for the form values searchDateFrom and searchDateTo. If there are then send those values to obtain your data source otherwise do the default. (see... https://codeigniter.com/user_guide/libra...controller )
(09-18-2018, 12:02 PM)Wouter60 Wrote: Add this to your controller function (at the top):
PHP Code: $datefrom = $this->input->post('searchDateFrom'); $dateto = $this->input->post('searchDateTo');
$config["base_url"] = base_url() . "/Report/searchDate"; $config["total_rows"] = $orders->record_count_search($datefrom,$dateto); // and a few lines further down: $data['data']=$orders->get_orders_search($config["per_page"], $page,$datefrom,$dateto);
Next, modify your model functions and make them accept the extra arguments $datefrom and $dateto.
PHP Code: public function record_count_search($datefrom,$dateto) { $this->db->select('*')->from('orders'); if ($datefrom AND $dateto) { $this->db->where('date >=' , $datefrom); $this->db->where('date <=' , $dateto); } return $this->db->count_all_results(); }
public function get_orders_search($limit, $offset, $datefrom, $dateto) { // similar as the other function, but returns array of records. }
Hi , thanks for the reply, i've tried both but still not working, whenever i clicked on the second page, the query runs without the `where` clause
-
cyjuice
Newbie
-
Posts: 5
Threads: 3
Joined: Sep 2018
Reputation:
0
Hi, thanks for all of your help !! I've tried a solution that use GET method instead of POST method that works.
|