Forums

[Solved] To add together the amounts in two fields. CCv4

vales 09 Feb, 2013
I want to add together the values of two numeric fields in a chronoconnectivity CCv4.

field1 and field2

In General -> Fields list (Optional) I put this: fields1+fields2 as fieldstot

In the Admin List Settings -> List View Fields I put this: fieldstot

When I launch Admin List I find in all rows of the table the string value "fieldstot"

It's possible to do this? There is an error?
Max_admin 10 Feb, 2013
Hi Vales,

If you have a custom listing (frontend) then you can add values using php in the body box:
<?php $value = $row['field1'] + $row['field2']; ?>


But if you are using the auto listing for admin or frontend, then you will have to use the form row event, assign an event to that, and use a custom code action in that form event to do the calculation, the row data is available under the $form->data array in that event:
<?php $form->data['tot'] = $form->data['field1'] + $form->data['field2']; ?>


Then you can use "tot" as the field name to show the total in your listing.

I hope this helps.

Best Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 10 Feb, 2013
Hi vales,

I don't know how good the List View Fields box is at handling MySQL expressions - you could test byt turning the CC Debugger on to see what the query looks like.

I'd probably use a custom listing and do the sums in the header or body box, but that may not be the best way.

Bob
vales 10 Feb, 2013
Thanks Max and Bob,
Great performance of Chronoconnectivity and ChronoForms!

I solved it by following the instruction of Max.

I created a new event called "tot" in the On Submit area of form attached to the connectivity in the Admin Editing->Admin Form.
I plugged in the event an action "Custom code" with this script:

<?php
$form->data['row']['total']=$form->data['row']['field1']+$form->data['row']['field2'];
?>

I added in the connectivity the field "total" in General-> Fields list (Optional)and Admin Setting List-> List View Fields
I put in connectivity in the Admin List Settings-> Auto list row event the value "tot"

Everything works perfectly.
RobP 30 Aug, 2013
I followed the example from Vales.
Unfortunately I run into some problems.

1- If I add the "total" field to the field list it only lists "field" in every row, not the values.

2- If I add the new calculated field name in general field list it gives a SQL error if I use model id's for the other fields.

Any suggestions?

Rob
RobP 02 Sep, 2013
What I'm trying to do is to make a calculated field age from a table field year_of_birth.

With the Custom list I put;

<?php $value = date("Y") - $row['ModelId']['year_of_birth']; ?>

in the body (before <tr> ) ,and
<td><?php echo $value; ?></td>

in the table, that works.

For the auto listing I made an event "agecalc" in the Chronoforms form with a custom code:
<?php
    $form->data['row']['age'] = Date("Y") -  $form->data['row']["ModelId']['year_of_birth'];    
?>


In the field Auto list row event I put: agecalc
and in the listing field: age

That does not work, I must have missed the right syntax.
Who can help me with this?

Rob
GreyHead 02 Sep, 2013
Hi Rob,

For the form version please try without ['row']

If you add a debugger to the form you can see the contents of the $form->data[''] array to check the correct names.

Bob
RobP 02 Sep, 2013
I tried that.

If I put the field age in the form listing, it displayd age in all the rows, not the values.
Can you help me with the correct syntax?

Rob
GreyHead 02 Sep, 2013
Hi Rob,

I've lost track of where you are using this. I thought it was in a ChronoForm called from your listing?

Bob
RobP 02 Sep, 2013
Hi Bob,

My apologies for the confusion.

The syntax I'm looking for is for the List View Fields in Front end list settings.

If I put age in the fields list it displays age in all the rows instead of the values (with auto listing).

Rob
GreyHead 02 Sep, 2013
Hi Rob,

I don't know ChronoConnectivity well enough to know if you can do a calculation with an Auto Listing :-( That would need to be a question for Max - you can try asking him through the Contact Us link above.

As soon as I need anything custom I switch to the Custom Listings where I have much more control and there I would expect your $value calculation to work.

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