Forums

List all records when the dropdown box is empty

AntonioGraca 09 Mar, 2013
Hello
I have a small problem .
I need to list the records according to the values ​​presented in three dropdown box (which are the list of other data tables ). So far so good. I can list the records according to the choice of filter dropdown box and still order under option radio. But I'd like to have the option to list all the records of a particular field, if the dropdown box is empty.
I searched but did not find anything that can show me the way ...

In submit, I have a custum code whith the next code to order records:
<?php
$Ordenar=$form->data['ordenar'];

if ($Ordenar == 'Data'){
  $form->data['order'] = 'data_marcas';
}
elseif ($Ordenar == 'Marca'){
  $form->data['order'] = 'marca_marcas';
}
?>


And, in submit, I have a DB Multi Record Loader whith a next code in where box to list the records under selected condition of three dropdown box:
<?php
$user =& JFactory::getUser();
$user_id = $user->id;
$atleta_select=JRequest::getVar('atleta_select');
$prova_select=JRequest::getVar('prova_select');
$epoca_select=JRequest::getVar('epoca_select');

?>

`cf_user_id` = '<?php echo $user_id; ?>' AND
`nome_marcas` = '<?php echo $atleta_select; ?>' AND `prova_marcas` = '<?php echo $prova_select; ?>' AND `epoca_marcas` = '<?php echo $epoca_select; ?>'


I'd like to have the option to list all the records of a particular field (eg. prova_marcas), if the dropdown box is empty. How to proceed?

Thanks and Have a nice weekend!

António Graça
GreyHead 09 Mar, 2013
Hi Antonio,

If I understnd correctly you have three drop-downs and any of them could be 'empty' - that is it will return a value of ''. Here's one way to do it:
<?php
$user =& JFactory::getUser();
$where = array();
if ( $user->id > 0 ) {
  $where[] = " `cf_user_id` = '{$user->id}' ";
}
if ( isset($form->data['atleta_select']) && $form->data['atleta_select'] ) {
  $where[] = " `nome_marcas` = '{$form->data['atleta_select']}' ";
}
if ( isset($form->data['prova_select']) && $form->data['prova_select'] ) {
  $where[] = " `prova_marcas` = '{$form->data['prova_select']}' ";
}
if ( isset($form->data['epoca_select']) && $form->data['epoca_select'] ) {
  $where[] = " `epoca_marcas` = '{$form->data['epoca_select']}' ";
}
if ( count($where) > 0 ) {
  $where = implode(' AND ', $where);
} else {
  $where = "1 = 1";
}
echo $where;
?>

This checks each drop down and adds it to the $where array if there is a value set; if there is nothing set then it sets $where to 1=1 which is always true and so will list all the results.

Bob
AntonioGraca 09 Mar, 2013
Fantastic, fantastic! It works perfectly!
Im thinking I was near the solution, but it was a little more complicated.😟

Have a Good Day, a Good Weekend and eternal life!😛
Many Thanks Bob
AntonioGraca 14 Mar, 2013
Hello Bob

In your solution, to solve my problem, everything ran well even be necessary to click on more pages to view moore records in list. Every time I load a new page (page 2, or 3 or next) the filters return to defaults (empty) and no records found.
What could be wrong?

Thanks
António Graça
This topic is locked and no more replies can be posted.