GetClickedInc
Total posts: 2
13 Дек 2014 12:42

I have been playing with Cobalt 8 for a few days, and so far I've been able to figure out everything I need it to do... It's great.

But... one thing I'm not sure if it's possible, or if it's easy to do, is this:

I have created a TYPE called "Sore", for example. Inside, there are several fields, such as "Apples", "Oranges", "Bananas", and "Pineapple". The user would type in a number in each field, so that there is a record of how many apples, oranges, that this store has. The user saves it and the record is saved.

Is there a way, when displaying the record to the website visitors, to automatically add up the number in each of the apples, bannans, fields fro this one record, and show the total number? For example, "This store has 12 fruits in total"? (I don't need to add up the totals from all the records, I juist need it added up from inside each "Store" individually.)

Basically, I want there to be several user-entered number fields, but then one field that can't be edited that will automatically add up the number and display the total?

How can this be achieved?

(Please note, I have very limited PHP and SQL knowledge, although I can guess that this could be done by using a custom template and then using SQL to find and calculate the number, however that is beyond my knowledge, while I know it's possible I have no idea how. I'm hoping there is a special field type I can buy or download that will do this much easier!)

Thank you!

Последние изменения: 15 Дек 2014


Jeff VIP
Total posts: 745
14 Дек 2014 23:52

GetClickedInc Please note, I have very limited PHP and SQL knowledge, although I can guess that this could be done by using a custom template and then using SQL to find and calculate the number, however that is beyond my knowledge

You don't need another field just to display the sum of fields, unless you want to search by 'number of fruits per shop'. Just create a custom template and add up the field values with php:

$sum_total = $this->fields[1]->result + $this->fields[2]->result;
echo 'This store has '.$sum_total.' fruits in total';

That's it!


Sergey
Total posts: 13,748
15 Дек 2014 09:18

Jeff you are right. Only small correction. result may contain HTML sometimes. Better use value.

$sum_total = $this->fields[1]->value + $this->fields[2]->value;

Jeff VIP
Total posts: 745
15 Дек 2014 09:26

Sergey Jeff you are right. Only small correction. result may contain HTML sometimes. Better use value.

I stand corrected :-)


GetClickedInc
Total posts: 2
15 Дек 2014 13:18

Thanks for the code - do I repalce [1] and [2] with the name of the field I gave it, such as [apples]?

Or do I need to somehow find the variable elsewhere to make it work?

$sum_total = $this->fields[apples]->result + $this->fields[oranges]->result; echo 'This store has '.$sum_total.' fruits in total';


Jeff VIP
Total posts: 745
15 Дек 2014 23:17

Please read this first: How do I know what is an ID of the field I need?

You need to replace the numbers between the brackets ([1] and [2]) with the id's of your fields.

Работает на Cobalt