从 codeigniter 中的 $_GET 数据在数据库中的最小值和最大值之间搜索

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

我有一个表单,提交后我的控制器从该表单获取数据并发送到模型以根据 _GET 信息从数据库中检索结果。

在我的表单中,有两个字段称为最小年和最大年,我使用它们在数据库中的年份列中搜索这些数字之间的字段。 _GET数组在提交后就像'min-year'=>'something''max-year'=>'something'

提交后_GET数组如'min-year'=>'something'和'max-year'=>'something'

Html:

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

控制器:

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

型号:

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

_GET 数组转换为 '->where()' 可读的内容以在所有其他列以及数据库中年份列的 min/max 之间进行搜索的最快方法是什么?

我需要一种正确的方法来创建 'where' 语句(字符串或数组),其中包含 所有其他输入字段以及 'between' 语句,如果可能的话,从 $_GET 信息中进行一次查询

如果min-year,max-year 或两者或其他任何字段都是空值提交,结果应基于其他字段,并且仍然可以从数据库中获取结果

请注意我还有很多其他输入字段,这只是表格的一部分。

在视图中

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

在控制器中

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'));


编辑 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
}

模型中

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

尝试this.It的控制器功能代码。将其粘贴到您的控制器函数中并更改字段名称,例如您的数据库 table 字段名称。

    $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'";

在模型函数中发送 where 子句。

您可以执行如下操作。

在控制器中获取不同变量中的两个值,如下所示:

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

然后将此变量传递到您的模型函数中,如下所示:

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

在查询之间的模型使用如下:

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

在 $result 中你会得到你的结果。

尝试this.It的控制器功能代码。将其粘贴到您的控制器函数中并更改字段名称,例如您的数据库 table 字段名称。

  $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'";

在模型函数中发送 where 子句。