hendrikbez Posted July 23, 2022 Share Posted July 23, 2022 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> Quote Link to comment https://forums.phpfreaks.com/topic/315078-how-to-sum-columns-that-is-in-sql-to-table-that-use-php/ Share on other sites More sharing options...
webdeveloper123 Posted July 23, 2022 Share Posted July 23, 2022 there is a SUM function in SQL, you can find it here https://www.w3schools.com/sql/sql_count_avg_sum.asp Quote Link to comment https://forums.phpfreaks.com/topic/315078-how-to-sum-columns-that-is-in-sql-to-table-that-use-php/#findComment-1598535 Share on other sites More sharing options...
benanamen Posted July 23, 2022 Share Posted July 23, 2022 Whenever you have consecutively numbered columns that is a big red flag that you have a bad database design. Stop what you're doing and learn database normalization. Quote Link to comment https://forums.phpfreaks.com/topic/315078-how-to-sum-columns-that-is-in-sql-to-table-that-use-php/#findComment-1598536 Share on other sites More sharing options...
webdeveloper123 Posted July 23, 2022 Share Posted July 23, 2022 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/315078-how-to-sum-columns-that-is-in-sql-to-table-that-use-php/#findComment-1598537 Share on other sites More sharing options...
hendrikbez Posted July 23, 2022 Author Share Posted July 23, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315078-how-to-sum-columns-that-is-in-sql-to-table-that-use-php/#findComment-1598539 Share on other sites More sharing options...
Barand Posted July 23, 2022 Share Posted July 23, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315078-how-to-sum-columns-that-is-in-sql-to-table-that-use-php/#findComment-1598540 Share on other sites More sharing options...
webdeveloper123 Posted July 23, 2022 Share Posted July 23, 2022 8 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. use the built in function : number_format. using the 2nd argument (decimals) should allow you to print out to 1.25 Quote Link to comment https://forums.phpfreaks.com/topic/315078-how-to-sum-columns-that-is-in-sql-to-table-that-use-php/#findComment-1598541 Share on other sites More sharing options...
Barand Posted July 23, 2022 Share Posted July 23, 2022 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) Quote Link to comment https://forums.phpfreaks.com/topic/315078-how-to-sum-columns-that-is-in-sql-to-table-that-use-php/#findComment-1598542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.