Forums

How to show sum of a field on form

pokomon 22 Oct, 2016
I'd like to show the sum of a field in the header of my form. Could you help me with the syntax of php?
GreyHead 22 Oct, 2016
Hi pokomon,

I'm sorry but I don't understand your question. Please give us some more detail about exactly what you need to do.

Bob
pokomon 22 Oct, 2016
In the header of my chronoforms-form I would like to show something like 'there are allready 30 of 60 seats reserved'. Where 30 is the sum of all reserved seats and seats is a field of my form and database. 60 is the maximum of seats but is not important, this is just text.
What is the syntax for showing 'there are allready' sum(seats) 'of 60 seats reserved'?
GreyHead 23 Oct, 2016
Hi pokomon,

In the form On Load action add a database query to get the number of bookings. You could use a DB Read action but Custom Code is probably as easy. Something like this
<?php
$db = \JFactory::getDBO();
$query = "
    SELECT COUNT(*)
        FROM `#__some_table` ;
";
$db->setQuery($query);
$form->data['bookings'] = $db->loadResult();
?>
Then add {bookings} in a Custom Code element in the Designer tab
<p>there are already {bookings} of 60 seats reserved.</p>

Bob
pokomon 23 Oct, 2016
Great! I would like to do this also in the chronoconnectivitypage. Is that possible?
GreyHead 23 Oct, 2016
Hi pokomon,

Yes I think so - you could add similar code to the Header box of the listing.

Bob
Kronosites 15 Nov, 2016
Hello!

I would add more información and to the same SQL with a "WHERE" user_id=logged user id.

For example:
    <?php
    $user =& JFactory::getUser();
    $form->data['ActualUserID'] = $user->id;
    $db = \JFactory::getDBO();
    $query = "
        SELECT COUNT(*)
            FROM `jos_kunena_user_topics` WHERE user_id={ActualUserID} ;
    ";
    $db->setQuery($query);
    $form->data['KunenaTemas'] = $db->loadResult();
    ?>


But of course it didnt work... could you help me?
Thanks.
But of
GreyHead 16 Nov, 2016
Hi Kronosites,

You can't use {variable_name} in PHP, please try
FROM `jos_kunena_user_topics` WHERE `user_id` = {$user->id} ;

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