nathan
Posts: 0
Joined: Mon Nov 18, 2013 6:16 pm

Create a Total from database items

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)

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Create a Total from database items

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

nathan
Posts: 0
Joined: Mon Nov 18, 2013 6:16 pm

Create a Total from database items

Yes I do want to do that

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Create a Total from database items

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

nathan
Posts: 0
Joined: Mon Nov 18, 2013 6:16 pm

Create a Total from database items

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

Nathan Grossman
Posts: 0
Joined: Mon Nov 25, 2013 6:17 pm

Create a Total from database items

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.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Create a Total from database items

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...

Nathan Grossman
Posts: 0
Joined: Mon Nov 25, 2013 6:17 pm

Create a Total from database items

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

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Create a Total from database items

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

Return to “Issues”