Forums

Event Switcher [GH], if and elseif

AntonioGraca 15 Apr, 2013
Hi
I use the Event Switcher [GH] to load content under various conditions. I can not understand, but something is not well.

I use Event Switcher [GH] element, with the following code "if"

if (!empty($count_prova1)):
   return 'event_a';
elseif (!empty($count_prova2)):
   return 'event_b';
elseif (!empty($count_prova3)):
   return 'event_c';
elseif (!empty($count_prova4)):
   return 'event_d';
endif;


But only work one option at a time, if option A is true no longer load the option B. But if option A is false already carries option B, and so on.
I would like to load all options that were true, but maybe something is wrong...馃樁

What am I doing wrong?
Someone can help me?

Thanks
Ant贸nio Gra莽a
GreyHead 15 Apr, 2013
Hi Ant贸nio,

What kind of options are you trying to load?

Bob
AntonioGraca 15 Apr, 2013
Hi, Bob

I need to find the personal best (time) of a given athlete in four distance (Prova1, Prova2, Prova3 and Prova4).
So I'm using Event Switcher [GH] with the code posted in the previous message. In each option Event Switcher [GH] load a DB Multi Record Loader to list the personal best of each distance selected by the user. Code of frame WHERE statement:

<?php
$user =& JFactory::getUser();
$user_id = $user->id;
$atleta_select=JRequest::getVar('atleta_select');
$prova_select1=JRequest::getVar('prova_select1');
$marca=JRequest::getVar(`marca_marcas`);
?>

`cf_user_id` = '<?php echo $user_id; ?>' AND
`nome_marcas` = '<?php echo $atleta_select; ?>' AND `prova_marcas` = '<?php echo $prova_select1; ?>' ORDER BY `marca_marcas` LIMIT 1


If I do not use the Event Switcher [GH] with the option to load the DB Multi Record Loader, wehn dont exist records listed, the header appears listed.

I think the Event Switcher [GH] is a good option, but only load one option at a time, not load the all optiont if all distance (Prova1, Prova2, Prova3 and Prova4) have records in table.

Thanks

Ant贸nio Gra莽a
GreyHead 16 Apr, 2013
Hi Ant贸nio,

The Event Switcher [GH] is really intended for when you need to change the sequence of events. Here you are just changing the logic of a MySQL query so I think you can do it better in a Custom Code action. Here's some code that I sketched out:
<?php
$user =& JFactory::getUser();
$user_id = $user->id;
$events = array(
  'prova_select1',
  'prova_select2',
  'prova_select3',
  'prova_select4',
);
$where = array();
foreach ( $event as $e ) {
  if ( isset($form->data[$e]) && $form->data[$e] ) {
    $where[] = $form->data[$e];
  }
}
$where = implode(', ', $where);
echo "`cf_user_id` = '{$user_id}' AND
`nome_marcas` = '{$atleta_select}' AND
`prova_marcas` IN ({$where})
ORDER BY `marca_marcas`;
";
?>
Not tested and probably needs debugging!!

Bob
AntonioGraca 17 Apr, 2013
Hi, Bob

Thanks for your help.

I apply de code in frame WHERE statement of DB Multi Record Loader. The code return same errors. I Debugg little things...
The final code:
<?php
$user =& JFactory::getUser();
$user_id = $user->id;
$atleta_select=JRequest::getVar('atleta_select');
$events = array(
  'prova_select1',
  'prova_select2',
  'prova_select3',
  'prova_select4',
);
$where = array();
foreach ( $events as $e ) {
  if ( isset($form->data[$e]) && $form->data[$e] ) {
    $where[] = $form->data[$e];
  }
}
$where = implode(',', $where);
echo "`cf_user_id` = '{$user_id}' AND
`nome_marcas` = '{$atleta_select}' AND
`prova_marcas` IN ({$where})
ORDER BY `marca_marcas` LIMIT 1";

?>


But, dont return data list...
I apply the debugger and the old code, working well but only an distance in query, the debugger return:

SELECT `nome_marcas`, `epoca_marcas`, `escalao_marcas`, `prova_marcas`, `marca_marcas`, `local_marcas`, `data_marcas`, `classificacao_marcas`, `competicao_marcas`, `observacoes_marcas` FROM `apo_chronoforms_data_marcas_registo` AS `marcas_parametros_filtrar` WHERE `cf_user_id` = '65' AND `nome_marcas` = 'Mariana Cordeiro' AND `prova_marcas` = '800m' ORDER BY `marca_marcas` LIMIT 1


Whith the new code, whith several distance in query, the debugger return:

SELECT `nome_marcas`, `epoca_marcas`, `escalao_marcas`, `prova_marcas`, `marca_marcas`, `local_marcas`, `data_marcas`, `classificacao_marcas`, `competicao_marcas`, `observacoes_marcas` FROM `apo_chronoforms_data_marcas_registo` AS `marcas_parametros_filtrar` WHERE `cf_user_id` = '65' AND `nome_marcas` = 'Mariana Cordeiro' AND `prova_marcas` IN (100m,800m,1000m,1500m) ORDER BY `marca_marcas` LIMIT 1


I think the problem is here: `prova_marcas` IN (100m,800m,1000m,1500m)

We are close, I think...
Thanks for your help


Ant贸nio Gra莽a
GreyHead 17 Apr, 2013
Hi Antonio,

I missed the quotes around the array entries. It needs to be:
`prova_marcas` IN ('100m', '800m', '1000m', '1500m')


Bob
AntonioGraca 17 Apr, 2013
Hi, Bob
Make sense... but, in code, where I put the quotes? 馃樁

<?php
$user =& JFactory::getUser();
$user_id = $user->id;
$atleta_select=JRequest::getVar('atleta_select');
$events = array(
  'prova_select1',
  'prova_select2',
  'prova_select3',
  'prova_select4',
);
$where = array();
foreach ( $events as $e ) {
  if ( isset($form->data[$e]) && $form->data[$e] ) {
    $where[] = $form->data[$e];
  }
}
$where = implode(',', $where);
echo "`cf_user_id` = '{$user_id}' AND
`nome_marcas` = '{$atleta_select}' AND
`prova_marcas` IN ({$where})
ORDER BY `marca_marcas` LIMIT 1";

?>


Thanks again

Ant贸nio Gra莽a
AntonioGraca 17 Apr, 2013
Hello Bob.
I Solved the problem of the quotes in this way.

$where = implode("','", $where);
$where = "'".$where."'";


Now the code return by debugg is:
`prova_marcas` IN ('300m','800m','1000m','1500m')


But only appear the result of one distance. If I take the LIMIT 1 show all records of all distances and not just the best time in each distance.

Any idea?


Ant贸nio Gra莽a
GreyHead 18 Apr, 2013
Hi Antonio,

It will depend on how your Database table is organised, you may not be able to do that with a single query. Possibly you need one query for each distance.

Bob
AntonioGraca 21 Apr, 2013
Hello, Bob

Only to say, it's done! and to say Thanks.

First atempt: several DB Multi Record Loader, one per distance.. (load several header and when the result is empty load the header whithout data);

Second atempt: Event Switcher [GH], and several if and elseif (but dont return the personal best to each distance)

Last atempt: A Custom Code Action, whith a several query (and many code). Return the personal best of athlete in each distance, one row per distance, and whith single header. Its fine.

Thanks Bob

Ant贸nio Gra莽a
GreyHead 21 Apr, 2013
Hi Ant贸nio,

Bravo - well done :-)

Bob
This topic is locked and no more replies can be posted.