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

时间:2022-10-06 19:50:07

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.

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

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.

在我的表格中,有两个字段,分别是minyear和maxyear,我使用它们在数据之间的数据库中搜索year列。_GET数组就像' minyear '=>'something'和' maxyear '=>'在提交之后。

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

_GET数组就像' minyear '=>'something'和' maxyear '=>'在提交之后。

Html:

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?

将_GET数组转换为'->where()'的可读对象的最快方式是什么?

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

我需要一种合适的方法来创建“where”语句(字符串或数组),它包含所有其他输入字段和“between”语句,如果可能的话,所有这些都来自$_GET信息的一个查询

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

如果minyear、maxyear或两者或其他任何字段都是通过空值提交的,那么结果应该基于其他字段,并且仍然能够从数据库中获得结果

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

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

3 个解决方案

#1


1  

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

注意:我还没有测试这段代码。有时如果日期在$minyear中没有使用,则使用date()函数,如$minyear = date(date_format('Y-m-d', $min.'-01-01');


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

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

#2


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.

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

#3


0  

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

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

在$result中,您将得到结果。

#1


1  

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

注意:我还没有测试这段代码。有时如果日期在$minyear中没有使用,则使用date()函数,如$minyear = date(date_format('Y-m-d', $min.'-01-01');


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

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

#2


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.

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

#3


0  

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

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

在$result中,您将得到结果。