Jump to content

How to sum columns that is in SQL, to table that use php


hendrikbez

Recommended Posts

I do not know how to do the following, need help with it

Have a table in mysql with columns, but I need the following columns fees_1, fees_2, fees_3, fees_4 and fees_5, to be sum in my html table in only one column Fees_bnb.

Using php and javascript.

Any help on how to do this will be apricated

Did try this, but it only sum up the first fees1

I have try this two code, but still not working

 

1.

I do not know how to do the following, need help with it

Have a table in mysql with columns, but I need the following columns fees_1, fees_2, fees_3, fees_4 and fees_5, to be sum in my html table in only one column Fees_bnb.

Using php and javascript.

Any help on how to do this will be apricated

Did try this, but it only sum up the first fees1

<td>
    <?php
        $fees1 = $num1 = $checkSqlRow["FEES_1"];
        $fees2 = $num2 = $checkSqlRow["FEES_2"];
        $fees3 = $num3 = $checkSqlRow["FEES_3"];
        $fees4 = $num4 = $checkSqlRow["FEES_4"];
        $fees5 = $num5 = $checkSqlRow["FEES_5"];
        $fees6 = $num1 + $sum2 + $sum3 + $sum4 + $sum5;
        echo $fees6;
   ?>
</td>   

2.  

<td>								
	<?php
	   $checkSql = mysqli_query("SELECT FEES_1, FEES_2, FEES_3, FEES_4, FEES_5,
								FEES_1 + FEES_2 + FEES_3 + FEES_4 + FEES_5 AS Total FROM nmc_my_munte");
										
		$total = total;
		echo $total;
	?>
</td>	

 

Link to comment
Share on other sites

29 minutes ago, hendrikbez said:
$fees1 =

 

I dont think you need to do that, (fees1 to fees5,) just save the values of fees1 to fees5 in variables, like $num1 and $num2 then just add each variable like you would do in maths. btw 

 

33 minutes ago, hendrikbez said:
  $fees6 = $num1 + $sum2 + $sum3 + $sum4 + $sum5;

that doesn't seem right, You got $num1 then you add it to $sum2 and $sum3 etc, but you dont have $sum2 anywhere in a variale

  • Like 1
Link to comment
Share on other sites

11 minutes ago, webdeveloper123 said:

that doesn't seem right, You got $num1 then you add it to $sum2 and $sum3 etc, but you dont have $sum2 anywhere in a variale.

Thank you, blind like a bat, did change all the $sum to $num, it is working, just do not show correct in html table (if I do 1.15 + 0.03 + 0.04 + 0.01 + 0.02 it should be 1.25 but it shows 1.2.

Link to comment
Share on other sites

SUM won't work with a dog's breakfast of a table like that one - it needs normalizing.

if the table were to be normalized to

table : fee
 

+--------+-----------+---------+----------+
| id (PK)| nmc_id(FK)|  seq    | amount   |
+--------+-----------+---------+----------+
|    1   |    1      |    1    |   100    |
|    2   |    1      |    2    |    75    |
|    3   |    1      |    3    |   150    |
|    4   |    1      |    4    |   225    |
|    5   |    1      |    5    |    50    |
|    6   |    2      |    1    |   125    |
|    7   |    2      |    2    |    85    |
|    8   |    2      |    3    |   350    |
|    9   |    2      |    4    |   215    |
|   10   |    2      |    5    |   125    |
+--------+-----------+---------+----------+

with 5 fee records for each parent nmc record then you can use SUM()
 

SELECT nmc_id
     , SUM(amount) as fees
FROM fees
GROUP BY nmc_id

 

Link to comment
Share on other sites

4 minutes ago, hendrikbez said:

(if I do 1.15 + 0.03 + 0.04 + 0.01 + 0.02 it should be 1.25 but it shows 1.2.

Better show your code

echo 1.15 + 0.03 + 0.04 + 0.01 + 0.02;

gives me 1.25

mysql> select * from a_sample;
+------+------+------+------+------+
| fee1 | fee2 | fee3 | fee4 | fee5 |
+------+------+------+------+------+
| 1.15 | 0.03 | 0.04 | 0.01 | 0.02 |
+------+------+------+------+------+
1 row in set (0.00 sec)

mysql> select fee1
    ->      , fee2
    ->      , fee3
    ->      , fee4
    ->      , fee5
    ->      , fee1 + fee2 + fee3 + fee4 + fee5 as total
    -> from a_sample;
+------+------+------+------+------+-------+
| fee1 | fee2 | fee3 | fee4 | fee5 | total |
+------+------+------+------+------+-------+
| 1.15 | 0.03 | 0.04 | 0.01 | 0.02 |  1.25 |
+------+------+------+------+------+-------+
1 row in set (0.03 sec)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.