Pandu Dewanta
Pandu Dewanta

Reputation: 35

How do I get results in my date filter?

I want to create a date filter using the form fields FROMDATE and TODATE.

This will provide the result as a range from start date to end date.

Here's my model

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

My issue is I do not see any results. Does anyone see what my mistake is?

Upvotes: 1

Views: 48

Answers (1)

skyyler
skyyler

Reputation: 165

Possible problem #1

If you declare a table's short name, then you have to use it. You declared the acc table's short name as a. So you have to use it in the like and where like this:

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

Possible problem #2

Maybe your problem is the DATE's format. I assume that you use DATE as the row type in your database. Your DATE row probably use the YYYY-mm-dd format. If your $from or $to variable don't use this format, this query will be incorrect.

You can check it by print it to your controller (and then die();), or into your log file. You can log anything from anywhere with the command (if you enable it in your application/config.php):

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

Log files are located in application/logs folder.

Tip:

It is better to declare the if statement with AND (&&) operators, because in this function you have to declare all of the variable.

I hope I could help you solve this problem.

Upvotes: 1

Related Questions