Reputation: 151
I'm paginated results in a table. Now add the way to filter the data, one of the ways of filtering is by the creation date, the user can enter a date "from" and also a date "to". I think the way I'm doing this fails, when I get a post verficiando that use data types the user to filter. Armo an arrangement with the conditions and the field that filter and eventually return them to page. I leave my code
if ($this->request->is('post')) {
if(isset($this->request->data['LogsAccione']['user_id'])){
$options['conditions']['LogsAccione.user_id'] = $this->request->data['LogsAccione']['user_id'];
}
if (isset($this->request->data['LogsAccione']['created_from']) && !empty($this->request->data['LogsAccione']['created_from']) && isset($this->request->data['LogsAccione']['created_to']) && !empty($this->request->data['LogsAccione']['created_to'])) {
$options['conditions']['LogsAccione.created'] = array(
'>=' => date('Y-m-d 00:00:00', strtotime($this->request->data['LogsAccione']['created_from'])),
'<=' => date('Y-m-d 23:59:59', strtotime($this->request->data['LogsAccione']['created_to']))
);
}
}
Obviously this failure, but can not find how to do it the way I want. Create an arrangement with multiple options for after filter and display to the user. In the particular case of the user, it works because there is only one option and that is the same, but in the case of the creation date is Between two possibilities.
Upvotes: 0
Views: 54
Reputation: 151
Solution :
if ($this->request->is('post')) {
if(isset($this->request->data['LogsAccione']['user_id']) && !empty($this->request->data['LogsAccione']['user_id'])){
$options['conditions'][] = "LogsAccione.user_id ='".$this->request->data['LogsAccione']['user_id']."'";
}
if (isset($this->request->data['LogsAccione']['created_from']) && !empty($this->request->data['LogsAccione']['created_from']) && isset($this->request->data['LogsAccione']['created_to']) && !empty($this->request->data['LogsAccione']['created_to'])) {
$options['conditions'][] = "LogsAccione.created >='".date('Y-m-d 00:00:00', strtotime($this->request->data['LogsAccione']['created_from']))."'";
$options['conditions'][] = "LogsAccione.created <='".date('Y-m-d 23:59:59', strtotime($this->request->data['LogsAccione']['created_to']))."'";
}elseif(isset($this->request->data['LogsAccione']['created_from']) && !empty($this->request->data['LogsAccione']['created_from'])){
$options['conditions'][] = "LogsAccione.created >='".date('Y-m-d 00:00:00', strtotime($this->request->data['LogsAccione']['created_from']))."'";
}elseif(isset($this->request->data['LogsAccione']['created_to']) && !empty($this->request->data['LogsAccione']['created_to'])){
$options['conditions'][] = "LogsAccione.created <='".date('Y-m-d 23:59:59', strtotime($this->request->data['LogsAccione']['created_to']))."'";
}
}
Upvotes: 1