Calculate sum of field using stackapps


(Daniel Manchester) #1

I’m trying to display the sum of entries in a field based on values in another field/s.

I’ve using the details stack and richtext field stack to get it to select the correct data using the ‘Where’ statement and using the code

%field(inventory_value)%

it correctly displayed the first entry that meets the criteria (it worked)

To get the sum working i tried %sum(inventory_value)% but that didn’t work, any ideas what would be the correct code?

Am I going about this the right way? with the details/richtext field stack?


(Daniel Manchester) #2

Tried taking a different track by inserting html/php code stack

I can get it to connect to the database using the code below

<?php $dbhost = 'localhost:3306'; $dbuser = '*******'; $dbpass = '*********' ; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if (! $conn ) { die('could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($conn); ?>

This works and displays the text ‘Connected successfully’

but as i have next to no php knowledge i tried the code

<?php $dbhost = 'localhost:3306'; $dbuser = '********'; $dbpass = '*********' ; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if (! $conn ) { die('could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql> SELECT SUM(inventory_value) -> FROM inventory WHERE student = session(userid) AND sub_category = "safety" mysql_close($conn); ?>

but it crashed, being php I hope this would be an easy answer fro someone now