Sum different values and insert the sum into the db

Sum different values and insert the sum into the db

andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

Hi,
i have this values in my Json

"movimenti": [
        {
          "id": "389",
          "movvar": "113"
        },
        {
          "id": "441",
          "movvar": "50"
        }

I need to sum them in a new field like

"movimenti-sum": [
        {
       
          "movvarsum": "163"
        },

and insert "mavvarsum" into the DB.

Directly in the server-side (php) file.
Ideas?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Hi,

    I'm not really seeing how all of this fits together I'm afraid. What I get from the above is that you load JSON from the server, want to sum the movimenti[].movvar properties and then submit that value to the server to be written to the database? I don't really get that I'm afraid.

    Allan

  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    Hi allan :)
    I'll make it easier and less scary.

    I must store in the db a calculated value...
    Can u help me? :)

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    I still don't really understand what the goal is here. Are you summing data from one table and storing it in another? Why do that when you could just sum it?

    Allan

  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    so i'm working on a tool to manage a warehouse.
    I have a table with the items, sku, description and the available quantity.
    Also i have a table with the movement. Everytime i put in or put out an item i made a record. I created another table where I write the id of the movement and the id of the item.
    Everytime I open the table with the item i calculate on the fly (with javascript) the number of the pieces i have in the warehouse.
    But i'd like to store this number also in the db because i need it in a lot of tables and i can retrieve it with a simple leftjoin...

    this is the items table
    http://temisgest.it/tg/files/inline/temis-disponibilita.php

    this is the table with the movements
    http://temisgest.it/tg/files/inline/temis-movimenti.php

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    My suggestion would be that you use a trigger in your database that will activate on any INSERT or UPDATE - that trigger would calculate the sum and write the value into your sum storage table.

    The other option is to use a server-side event which would basically do the same as a trigger.

    Allan

This discussion has been closed.