[SOLVED] if and else in calc field?

tusa

Member
I'm trying to use if statement in a calc field, like this:
Code:
$num = (int) '2';

if ('{nemmanus_elementer___elementer_nummer}' > $num) {
$thisDur = new DateTime('{nemmanus_elementer___elementer_duration}');
$thisDurFormat = $thisDur->format('H:i:s');
return $thisDurFormat;
} else {
$db = JFactory::getDbo();
$query = "SELECT TIME_FORMAT(SEC_TO_TIME(TIME_TO_SEC('{nemmanus_elementer___elementer_manus_time}') + TIME_TO_SEC('{nemmanus_elementer___elementer_duration}')), '%H:%i:%s')";
$db->setQuery($query);
$duration = $db->loadResult();
return $duration;
}
Am I doing anything wrong or why doesn't it work?

Best regards
Tue.
 
if and else are working just fine in calc elements.
So, yes, you must be doing something wrong in your code. At first glance it looks funky, indeed... but can't say more without more context and details, other than:
- You seem to have trouble with date/time formats again (as in your other thread), so you'll probably want to learn more on this.
- Avoid "detours" and unnecessary code, as it doesn't do anything but cause confusion and increase the risk of errors.
- Also, while sometimes pointers or even snippets may be given here (see your other thread), this is the Fabrik support forum, not one for PHP, SQL or else in general, or a place to have draft code verified for syntax ... there are many others better for this.
 
I've got it working with this code.
PHP:
$num = (int)'2';

if ('{nemmanus_elementer___elementer_nr}' < $num) {
$thisDur = new DateTime('{nemmanus_elementer___elementer_duration}');
$thisDurFormat = $thisDur->format('H:i:s');
return $thisDurFormat;
} else {

$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);

$myQuery
    ->select('elementer_akkumuleret')
    ->from('nemmanus_elementer')
    ->where('elementer_nr = ' . $myDb->quote('{nemmanus_elementer___elementer_nr}' - 1));

$myDb->setQuery($myQuery);
$myDbElementAkk = $myDb->loadResult();
$elementAkkFormat = strtotime($myDbElementAkk); // convert elementer_akkumuleret to Unix Epoch Time in seconds

$elementDur = new DateTime('{nemmanus_elementer___elementer_duration}');
$elementDurStr = $elementDur->format('Y-m-d H:i:s');
$elementDurFormat = strtotime($elementDurStr); // convert elementer_duration to seconds

$timestamp = strtotime('01:00') + 60*60;
$resultSecs = $elementAkkFormat + $elementDurFormat + $timestamp; // add them up

return date("H:i:s", $resultSecs); // convert Unix Epoch Time back to database format
}
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top