mrt003003 Posted May 1, 2011 Share Posted May 1, 2011 Hi there i have 4 fields in a record and 3 records in a table that i need to add up. Heres what they look like when outputted: 0024 0103 1126 Each of the fields that need to be added are called: Class1, Class2, Class3, Class4 So i want to add each number in those fields with the result of the records so above would look like: 1, 2, 4, 13 Would i need to put them in an array of somekind?? $colname_resultp = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_resultp = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query_resultp = sprintf("SELECT Class1, Class2, Class3, Class4 FROM planet WHERE PlayerName = %s", GetSQLValueString($colname_resultp, "text")); $resultp = mysql_query($query_resultp, $swb) or die(mysql_error()); $row_resultp = mysql_fetch_assoc($resultp); $totalRows_resultp = mysql_num_rows($resultp); <?php do { ?> <?php echo $row_resultp['Class1']; echo $row_resultp['Class2']; echo $row_resultp['Class3']; echo $row_resultp['Class4']; ?> <?php } while ($row_resultp = mysql_fetch_assoc($resultp)); mysql_free_result($resultp); ?> If you could please, please help that would be great as im finding this a real headache at the moment Thank you Tom Quote Link to comment https://forums.phpfreaks.com/topic/235286-add-up-int-values-in-records/ Share on other sites More sharing options...
fugix Posted May 1, 2011 Share Posted May 1, 2011 You could use the sum() function in your mysql query Quote Link to comment https://forums.phpfreaks.com/topic/235286-add-up-int-values-in-records/#findComment-1209106 Share on other sites More sharing options...
mrt003003 Posted May 1, 2011 Author Share Posted May 1, 2011 Great stuff thank you.. So it would look something like this: $query_resultp = sprintf("SELECT Class1, Class2, Class3, Class4, SUM(Class1, Class2, Class3, Class) FROM planet WHERE PlayerName = %s", GetSQLValueString($colname_resultp, "text")); Because i only want the fields Class1, Class2, Class3, Class4 which are to be summed. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235286-add-up-int-values-in-records/#findComment-1209113 Share on other sites More sharing options...
mrt003003 Posted May 1, 2011 Author Share Posted May 1, 2011 Yay i got it working... I didnt relaise sum() function on its own couldnt sum multiple expressions that way. Heres what i did: $query_resultp = sprintf("SELECT SUM(Class1) As Class1_totla, SUM(Class2) As Class2_totla, SUM(Class3) As Class3_totla, SUM(Class4) As Class4_totla FROM planet WHERE PlayerName = %s", GetSQLValueString($colname_resultp, "text")); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235286-add-up-int-values-in-records/#findComment-1209142 Share on other sites More sharing options...
fugix Posted May 1, 2011 Share Posted May 1, 2011 Glad you figured it out Quote Link to comment https://forums.phpfreaks.com/topic/235286-add-up-int-values-in-records/#findComment-1209144 Share on other sites More sharing options...
mrt003003 Posted May 1, 2011 Author Share Posted May 1, 2011 Thanks fugix Quote Link to comment https://forums.phpfreaks.com/topic/235286-add-up-int-values-in-records/#findComment-1209146 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.