1
In my Cakephp project, there are two dropdown fields to do a database search, both as string’s. I need to add two more fields (both dates). In the code below (controller), I used 4 if’s to check the fields, when I add the date fields, I will need to create many more if’s or there is another easier way?
Regitrohorariocontroller.php
$horarios = array();
$cliente_id = (isset($this->request->data['RegistroHorario']['cliente_id']) ? $this->request->data['RegistroHorario']['cliente_id'] : null );
$usuario_id = (isset($this->request->data['RegistroHorario']['user_id']) ? $this->request->data['RegistroHorario']['user_id'] : null );
if($cliente_id != null && $usuario_id != null){
$horarios = $this->RegistroHorario->find('all', array(
'conditions' => array('User.id' => $usuario_id, 'Cliente.id' => $cliente_id),
'order' => 'RegistroHorario.data_fim DESC'
));
}elseif($usuario_id != null && $cliente_id == null){
$horarios = $this->RegistroHorario->find('all', array(
'conditions' => array('User.id' => $usuario_id),
'order' => 'RegistroHorario.data_fim DESC'
));
}elseif($usuario_id == null && $cliente_id != null){
$horarios = $this->RegistroHorario->find('all', array(
'conditions' => array('Cliente.id' => $cliente_id),
'order' => 'RegistroHorario.data_fim DESC'
));
}elseif($usuario_id == null && $cliente_id == null){
$horarios = $this->RegistroHorario->find('all', array(
'order' => 'RegistroHorario.data_fim DESC'
));
}
View:
schedules_manage.ctp
<?php echo $this->Form->create('RegistroHorario', array('class' => 'form-horizontal bucket-form', 'autocomplete' => 'off')); ?>
<div class="col-lg-3">
<?php echo $this->Form->select('RegistroHorario.cliente_id', array(null => 'Todos') + $clientes, array('empty' => false ,'div' => false,'label'=>false, 'class' => 'form-control m-bot15', 'width' => '10'));?>
</div><div class="col-lg-3">
<?php echo $this->Form->select('RegistroHorario.user_id', array(null => 'Todos') + $usuarios, array('empty' => false ,'div' => false,'label'=>false, 'class' => 'form-control m-bot15', 'width' => '10'));?>
</div>
<input type="submit" class="btn btn-success" value="Buscar" />
<?php echo $this->Form->end(); ?>
It worked, thanks!!
– Julyano Felipe
As in my previous question, I would mark it as useful, but I still don’t have 15 points to do it.. How much to own them I will mark, even more
– Julyano Felipe