如何在日期过滤器中获取结果


How do I get results in my date filter?

我想使用表单字段FROMDATETODATE创建一个日期过滤器。

这将提供从开始日期到结束日期的范围内的结果。

这是我的型号

function search($status,$from,$to){
  if( $status =="" || $from =="" || $to =="") {
   $this->db->select('a.*');
   $this->db->from('acc a');
  }
  if( $status !="" || $from !="" || $to !="") {
    $this->db->like('status', $status);
    $this->db->where('fromdate <=',$from);
    $this->db->where('todate >=',$to);
  }
  $result = $this->db->get();
  //echo $this->db->last_query();
  return $result->result();
}

我的问题是我没有看到任何结果。有人看到我的错误是什么吗?

可能的问题#1

如果您声明了一个表的短名称,那么您必须使用它。您将acc表的短名声明为a。所以你必须像这样在likewhere中使用它:

$this->db->like('a.status', $status);
$this->db->where('a.fromdate <=', $from);
$this->db->where('a.todate >=', $to); 

可能的问题#2

也许你的问题是日期的格式。我假设您使用DATE作为数据库中的行类型。您的DATE行可能使用YYYY-mm-dd格式。如果$from$to变量不使用此格式,则此查询将不正确。

您可以将其打印到控制器(然后打印到die();)或日志文件中进行检查。您可以使用命令从任何地方记录任何内容(如果您在application/config.php中启用它):

log_message("error", "this is the text of your log message from date: " . $from . ", and to date: " . $to);

日志文件位于application/logs文件夹中。

提示:

最好用AND(&&)运算符声明if statement,因为在这个函数中,您必须声明所有变量。

我希望我能帮你解决这个问题。