Jump to content

Add up int values in records


mrt003003

Recommended Posts

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 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.