Search code examples
databasecodeignitersearchmaxmin

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

  • In View

    <a href="<?php echo base_url() ?>Controller/Method/MinYear/MaxYead"></a>
    <a href="<?php echo base_url() ?>welome/diff/2013/2016"></a>
    

    In Controller

    public function diff($min, $max) # Informing method there can be 2 input parameter from URL.
    {
        $now = date("Y");
        $minYear = date_format('Y-m-d', $min.'-01-01');
        $maxYear = ($now == $max) ? date("Y-m-d") :  date_format('Y-m-d', $max.'-12-31');
    
        # in $maxYear if year is current year we cant take 2016-12-31. So we use Current timestamp.
        # If its not current year then we use 12-31. Ex 2014-12-31
    
    }
    

    Note: I have not tested this code with your purpose. Sometimes if date not worked in $minyear use with date() function like this $minYear = date(date_format('Y-m-d', $min.'-01-01'));


    EDIT 01

    public function diff()
    {
        $min = $this->input->get('min-year');
        $max = $this->input->get('max-year');
        $state = $this->input->get('state');  # updated
    
        $now = date("Y");
        $minYear = date_format('Y-m-d', $min.'-01-01');
        $maxYear = ($now == $max) ? date("Y-m-d") :  date_format('Y-m-d', $max.'-12-31');
    
        $data['cars'] = $this->carinfo_model->all_ad($minYear, $maxYear, $state);  # updated
    }
    

    In Model

    public function all_ad($minYear, $maxYear, $state)  # updated
    {
        $this->db->select(*);
        $this->db->from('cars');
        $this->db->where('field_name BETWEEN date($minYear) AND date($maxYear)');
        $this->db->where('field_name', $state ); # updated
        $query = $this->db->get();
        $result = $query->->result_array();
        return $result;
    }