Counting the sum of the values of cells

I have a database, and I need to count from the values of some cells and display them in the final.

For example, I have three fields, one I enter the value 10, the other the value 12, the third the value 5, so the count will be 27, and I need to specify this in a separate field.

Whether it is possible to do this with the help of code, or standard tools of Wix code?

Hi,

Welcome to WixCode.

You don’t say if the field for the total is also in the database collection, or is it only a display field on the screen.

Yisrael

The truth is, for now I have not adding that field yet, since I don’t know which one is better.

The point is, that customer can see the total value after filling the form.

I also have a database with several cells containing values I would like to have totaled in a “SUM” column.

Just like with the original question, let’s say there are cells valued at 3, 5, 10, 2. I want a new cell within the database to add up the values to 20.

To clarify for Yisrael, ideally I would like to see the total shown in the database and also displayed on the screen in a lightbox after the survey I have created is submitted. The lightbox is already created and I just want to add a field showing the sum total of the applicant’s results.

Thank you!

Hi,

There’s no built in option for that, but you can do that using code (bot showing the value and putting in the database).
See here how to create the sum field before adding an entry to database.

Then, you can either use code or connection to show the number that has been calculated.

Liran.

Thank you for the info, Liran. I’m too am trying to do exactly what is described in the original two questions but I’m struggling with writing the code to add the sum field. I’ve followed the “beforeInsert( )” guide to add a hook before a new field is inserted in the database but the actual code required for the newly inserted field to sum 15 other fields has become a roadblock for me as I’ve never written code.

Any chance you can provide some coding help to sum my database fields titled Q1, Q2, Q3…Q15, each returning a value 1-7?

-Taylor

Hi Taylor,

Note that beforeInsert() is having all the info that is submitted, so assuming you’re submitting all of Q1, Q2,… Q15 together (and that they are numbers), and having a column called ‘sum’ this will look like:

export function myCollection_beforeInsert(item, context) {
  let submittedItem = item;
  let sum = 0;
  
  //Summing up all the numbers:
  for (let key in item) {
    sum += item[submittedItem];
  }
  
  //Adding sum value to the inserted record:
  submittedItem.sum = sum;
  return submittedItem;
}

Liran.

Hi Liran,

Thank you so much about this information!

But there are also a question about code, for now I have a database, consisting of column “Primary Field” (text), several columns (number) and column “sum” (also number).

I’ve created a hook, used a code above (changing myCollection name), but when I trying to fill the fields in Preview mode, in the “sum” field it showing “null” instead of sum:


Is there are something should be done before?

Thanks, Liran.

The Q1, Q2…Q15 data is all submitted together along with some more basic text data like name, age, etc.

Since I’m only interested in adding together the number values (Q1 - Q15), would I need to create an array or name each individual column for which i want to sum? With the code you’ve so generously provided, I’m only getting “sum” field created for new entries, no calculating is taking place.

Hi Alissa,

Note that my code goes over all the submitted data. If you want to take only the numbers, you should change it a bit:

export function myCollection_beforeInsert(item, context) {
  let submittedItem = item;
  let sum = 0;
  
  //field I need to sum:
  let fieldToSum = ['f1', 'f2', 'f3'];
  
  //Summing up all the numbers:
  for (let key in item) {
    if (fieldToSum.includes(key)){
      sum += item[submittedItem];
    }
  }
  
  //Adding sum value to the inserted record:
  submittedItem.sum = sum;
  return submittedItem;
}

Liran.

Taylor,

I’m not sure I understand… If you want to specify the fields, see my previous comment here.

Liran.

Liran,

I wanted to specify one thing in your code, in “fieldToSum = [ ‘f1’, ‘f2’, …]” instead of “f” I need to write names of my fields?

And the result will be showing in last column, which name is “sum”?

Thank you in advance.

Yes

Hi Liran,

That was exactly what I needed. It worked perfectly, thank you!

The next step for me is to link this new field from the live database to the text box to show the score after the completeion of the survey. I can get it work as intended in the sandbox version but not in the live version. I read in Wix Support that I am unable to link a created field like my new [sum] field to the schema in the live database, only in the sandbox database. Is that true?

Sorry for the delay, but I must be doing something wrong, I still have “null” in sum.

What I did:

  1. Create a database. First field “Region” is text, and the others is numbers.
  2. Add a hook “Before Insert”
  3. Add code.

But it still shows “null”

Hello Dear, I am going to sum of the values but didn’t get the total sum ?
i have radio buttons with range of 0-4 points, when i do enter them they inserted into the db.
i followed your code below code, but didn’t get the total sum in my db sum field. it shows me 0.
what should i do ? can radio button return text or number format ? please solved my this issue,

export function vopros_survey_beforeInsert(item, context) {

let submittedItem = item;
let sum = 0;

//field I need to sum:
let fieldToSum = [‘dg_stg_vs_Q1’, ‘dg_stg_vs_Q2’, ‘dg_stg_vs_Q3’, ‘dg_stg_vs_Q4’, ‘dg_stg_vs_Q5’];

//Summing up all the numbers:
for (let key in item) {
if (fieldToSum.includes(key)){
sum += item[submittedItem];
}
}

//Adding sum value to the inserted record:
submittedItem.sum = sum;
return submittedItem;
}

Hi to all
I am a new member and a beginner in writing code
I have this database


and this page


I want when the page opens
in the “Num Database records” box, display the number of database records (rows)
and when I press Submit Button after a new record to display the new number of database records (rows).

Thank you in advance.
Aris

Hey, did anyone ever figure this out? I’m in the same boat as the previous queries and have followed the same steps adding the hook and code, but i have nothing showing in my ‘Sum’ column.

Appreciate any help
cheers

Hey Liran,

Does this not work for a Submit button? Is it only valid when using an insert function?

I coded this into a quiz and I want to calculate the final score by adding the values of the radioButtons selected. However, the sum column is always empty.

I tried creating a button with an insert export function, but in that case all values are converted to strings, and therefor cannot be summed.

Same happening to me! Please help if you figured it out.