'Search between min and max in database from $_GET data in codeigniter

I have a form that after submission my controller gets data from that form and sends to model to retrieve a result from database based on the _GET information.

In my form there are two fields called min-year and max-year that i use them to search in year column in the database between those numbers. The _GET array is like 'min-year'=>'something' and 'max-year'=>'something' after submission.

The _GET array is like 'min-year'=>'something' and 'max-year'=>'something' after submission.

Html:

<input type='text' name='state' />
<input type='text' name='min-year' />
<input type='text' name='max-year' />

controller:

$this->load->model('carinfo_model');
$where=($this->input->get());
$data['cars']=$this->carinfo_model->all_ad($where);

model:

public function all_ad($where){
->select("*")
->from("cars")
->where($where)         
$result=$this->db->get()->result_array();
return $result;
}

What is the fastest way to transform the _GET array into something readable for '->where()' to search in all other columns plus between min/max in year column in database?

I need a proper way to create a 'where' statement(string or array), that contains all other input fields plus a 'between' statement, all in one query from $_GET information if it's possible

If min-year,max-year or both of them or any other fields were submitted by empty value, result should be base on other fields and should still be able to get the result from database

Please note i have so many other input fields and this is just part of the form.



Solution 1:[1]

Try this.It's a controller function code. Paste this in your controller function and change the field name like your database table field name.

    $a = $this->input->get();
    $state = $a['state'];
    $min_yr = $a['min-year'];
    $max_yr = $a['max-year'];

    $where = "(field_name BETWEEN '$min_yr' AND '$max_yr') AND state = '$state'";

Send the where clause in your model function.

Solution 2:[2]

You can do some thing like below.

In controller get both value in different variable as given below:

$min_year = $this->input->get('min_year');
$max_year = $this->input->get('max_year');

then pass this variable into your model function like below:

$this->model_name->function_name($min_year,$max_year);

In model use between query as given below:

$sql = "SELECT * FROM table_name
WHERE year BETWEEN $min_year AND $max_year";
$result = $this->db->query($sql)->get()->result_array();

In $result you will get your result.

Solution 3:[3]

Try this.It's a controller function code. Paste this in your controller function and change the field name like your database table field name.

  $a = $this->input->get();
    $state = $a['state'];
    $min_yr = $a['min-year'];
    $max_yr = $a['max-year'];

    $where = "(field_name BETWEEN '$min_yr' AND '$max_yr') AND state = '$state'";

Send the where clause in your model function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sharmistha Das
Solution 2
Solution 3 Soham Jadav