Getting previous record using calc element

tmntmn

Member
Hi All

I'm using this code to get the value from previous record in a list sorted by placeholder {fab_datatester___fly_reg_raw} eg different last record depending on the place holders value.
This works fine.

But the problem is, it only works correct for adding new records, if an existing record is edited in any field the calc element is calculated again using the new "wrong" value and not the previous value.


Code:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// selecting the tacho_slut field
$query->select('tacho_slut');
// from table fab_master_flyvedata
$query->from($db->quoteName('fab_master_flyvedata'));
//sorting the result by fly_reg placeholder id
$query->where($db->quoteName('fly_reg')." = ".$db->quote({fab_datatester___fly_reg_raw}));
// getting the last record
$query->order($db->quoteName('landings_tid'). 'desc LIMIT 1');
$db->setQuery($query);
return $db->loadResult();

How can I solve this?

Best regards

Thomas
 
hi - should the query not also order by {fab_datatester___fly_reg_raw} as well?
 
Hi Rob.

I'm not sure I understand what you mean? I'll try to explain it in more details.

I've a table/list where I record tacho times, one start_tacho and one end_tacho in each record.

The start_tacho in a new record is the end_tacho from the previous record. These tacho records are on different plane engines. They are defined in the list fab_datatester___fly_reg_raw. So the code works Ok for new records, the correct tacho_start time is found from the list sorted by the fab_datatester___fly_reg_raw. for the right engine.

The problem begins if someone makes a mistake and it is necessary to edit a previous record lets say 3 records back.

Obvious it's only the tacho_end that can be edited in the mistaken record, but at the same time the calc element does one more calc and saves the value not from the record 3 number back but from the newest record, as it is the latest.

I think that it might be solve by an if/then solution.

Like

If the tacho_end = NULL then use the calc code for tacho_start ( In a new record there will be no tacho_end yet)
else don't

I just need help to get the syntax right

Best regards

Thomas
 
Well the reason that original query gets it wrong on edit is, it's sorted by the main id (as far as I can tell), so you'll get the last record added, not the "row prior to this row". The "row prior to this row" (which is what you need) is only going to be the "last added" when inserting a new row.

So I'm guessing you need to do something like ...

Code:
if (empty('{fab_datatester___tacho_start}') {
  // your existing code goes here
}
else {
  return '{fab_datatester___tacho_start}';
}

That is, if it is the tacho_start which is this calc element. I'm still a little unclear on your setup.

I'm also not a 100% sure if, for a calc element, the placeholder will contain the previously calculated value when editing a row. But try it and see.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top