Page 1 of 2

Create a Total from database items

Posted: Mon Nov 18, 2013 6:16 pm
by nathan

I am trying to build a personal budget app. I am looking to see if I could find the total of the $ amounts that are saved to a database. The user should be able to add amount they spend on an item(which is added to a database collection). Then they should be able to see a label that shows the total amount they spend( sum of all database items)


Create a Total from database items

Posted: Mon Nov 18, 2013 7:30 pm
by maxkatz

Do you want to sum all the items in the database?


Create a Total from database items

Posted: Mon Nov 18, 2013 7:44 pm
by nathan

Yes I do want to do that


Create a Total from database items

Posted: Mon Nov 18, 2013 7:45 pm
by maxkatz

You can use Server Code to sum all the amounts in your database collection.


Create a Total from database items

Posted: Mon Nov 18, 2013 9:53 pm
by nathan

I am unfamiliar how to use server code. Is there a tutorial somewhere that explains how this could be done


Create a Total from database items

Posted: Mon Nov 18, 2013 10:05 pm
by maxkatz

Create a Total from database items

Posted: Mon Nov 25, 2013 6:17 pm
by Nathan Grossman

I have changed around my app a little and the server code seems a little hard for what I am doing, just a reminder I know very little javascript. All I need to know now is how do I save the total of a list database column to a variable. I have this part so far: var total_expense = JSON.Budget_expense_list_service(amount);. total_expense is the var I am creating which should have a float value i.e. 57.65. I have a service from a database called "Budget_expense_list_service". Within this database is a column called "amount" which has individuals float numbers. What I need to do is have total_expense be the sum of all of the rows in the amount column.


Create a Total from database items

Posted: Mon Nov 25, 2013 8:21 pm
by Maryna Brodina

Hello! This code prevar total_expense = JSON.Budget_expense_list_service(amount);/pre is not correct. There are few ways how you can implement it.
1) Use regular service and count total_expense on service success event. In success function all data is returned to data variable. You can go through this variable and sum necessary items. There is some disadvantage - in app you receive not one total_expense number, but all data for specific user. It leads to traffic increase. If that's not a problem (user doesn't have too many records and it's total size wouldn't be more than a few KB) it's the easiest way.
2) Do you use Appery.io back-end? If so you can use server code - here is more information http://docs.appery.io/documentation/b.... If you don't use Appery.io back-end this should help http://docs.appery.io/documentation/b...


Create a Total from database items

Posted: Wed Dec 04, 2013 6:16 pm
by Nathan Grossman

Could possibly supply the code I need to use server code. So far I have this,
precode
var database_id = "52839b58e4b0adb2eff0848d&quot
var result = {};

result = Collection.distinct(database_id,"Budget1","Amount", null, null);

response.success(
result
);
/code/pre

the final code needs to have a variable the sum of a column in a database which could be mapped to a label in my app


Create a Total from database items

Posted: Thu Dec 05, 2013 8:47 am
by Maryna Brodina

Hello! Try this server code prevar DB_id='xxxxxxxxxxxxxxxxxx',
collectionName='test',
columnName = 'amount';

try {
query = Collection.query(DB_id, collectionName);
var i, len, value, sum = 0;
for(i = 0, len = query&#46;length; i < len; i++) {
value = parseFloat(query[columnName]);
if (value) {
sum += value;
}
}
response&#46;success({totalSum: sum}, "application&#47;json");

} catch (e) {
response&#46;success({message: e&#46;message, code: e&#46;code}, "application&#47;json");
}/pre