Buy Now
Sign in

Sum of money

NabiXL , December 04 at 06:29
N
NabiXL
I am having a problem with a piece of code and its result.I have a list of prices on my table and i want the sum to be displayed at the bottom but the problem is that, i am not able to get the correct sum. Here is the code; and i uploaded the result alsoTable.JPG
<?php​
​$db =& JFactory::getDBO();​
​$query = "SELECT CONCAT('P', format(SUM(`price`),2)) AS `total_price`FROM `#expenditure`";​
​$db->setQuery($query);​
​$total_price = $db->loadResult();​
​echo "<strong>";​
​echo "Total Cost:";​
​echo $total_price;​
​ echo "</strong>";​
​?>
healyhatman
OK so first of all why are you doing it in PHP instead of just using a read data action, and secondly why are you using concat and multiple echos instead of just
echo "<strong>Total Cost: </strong>P$total_price</strong>";
and thirdly no idea What happens if your remove all the Concat stuff out of interest
Web developer at SkySpider.com.au
I don't work for ChronoEngine but I do accept donations paypal.me/healyhatman
N
NabiXL
I am a newbie in PHP, Please help if there is a shortcut of doing it. I wrote this code at the footer of the table..see image. I will reduce it.footer.JPG
Attachments
footer.JPG
footer.JPG
(74.07 KiB)
39 Downloads/Views
N
NabiXL
I am using CONCAT to add "P" as the currecy symbol to the price
N
NabiXL
Code simplified; Is there a simple way of doing it..Table2.JPG
<?php​
​$db =& JFactory::getDBO();​
​$query = "SELECT SUM(`price`) AS `total_price`FROM `#expenditure`";​
​$db->setQuery($query);​
​$total_price = $db->loadResult();​
​​
​echo "<strong>Total Cost: </strong>P$total_price</strong>"​
​?>
Attachments
Table2.JPG
Table2.JPG
(32.4 KiB)
36 Downloads/Views
healyhatman
Is this in CCv6 or CCv5?
And I don't know why it would be giving you the wrong figure. What comes up if you just list the price field without SUMming? Just to make sure it's returning the right values
Web developer at SkySpider.com.au
I don't work for ChronoEngine but I do accept donations paypal.me/healyhatman
N
NabiXL
Its CCv5, so the data type of Price is VARCHAR in my MyPHPAdmin. Can this affect the results? C/z when i run the query there, it gives me the same result which is "21"
healyhatman
You will need to cast it to a number first.
Web developer at SkySpider.com.au
I don't work for ChronoEngine but I do accept donations paypal.me/healyhatman
N
NabiXL
Sorry it gives the same result as 21, without summing it gives me "2" the first digit of the first price.
healyhatman
What's your new query
Web developer at SkySpider.com.au
I don't work for ChronoEngine but I do accept donations paypal.me/healyhatman