How can i add two column as totol

QuestionsHow can i add two column as totol
Chike asked 11 years ago

How can i add two colum.
i have a table called scores with colum ca, exam and total. every tin is working fine. i want to be able to add the database vaue of ca and exam (ca + exam) and display the output in the total colum pls help

3 Answers
Abu Ghufran answered 11 years ago

Hello,

Quickest solution would be to put sum in SQL and make total column. After that you can use that column in grid to display sum data.

e.g.

$g->select_command = "SELECT ca,exam, ca+exam as total FROM mytable";

later in grid code, you can define:

$col["name"] = "total";

Chike answered 11 years ago

Hi Abu,
i i had implemented this suggestion previously as such
$g->select_command = "SELECT sn, stud_name, stud_id, ca, exam, grade, remark, (ca+exam) as total FROM scores

the problem is that when i display the addition works
eg
ca exam total
10 30 40

the sql code above displays. But when i change the values
ca exam total
15 30 40

after refresh i get this
ca exam total
15 30 45

which is correct.
but the value for 'total' in the database table did not change automatically. Is there anyway i can automatically perform the addition between ca+exam and it update in the 'total' column of the database table automatically

plseas help me out
thanks

Abu Ghufran answered 11 years ago

Hello,

Option 1) Use mysql trigger to perform addition on update/insert and save in that field. See mysql triggers help for more.
Option 2) You can have custom events in phpgrid (on_update / on_insert) where you can perform the work and set in total field to be saved in db. Custom events are available in licensed version.

Your Answer

3 + 15 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?