danyo
danyo

Reputation: 5846

php subtracting data

I am trying to subract data in php.

I currently have 2 functions which generate 2 different values pulled in from the database.

function total_points() { 
    $query = "SELECT userid, SUM(value) FROM wp_scloyalty GROUP BY userid"; 
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    echo $row['SUM(value)'];
    }
}



function total_prizes_value($author) { 
    global $post;
    $info = get_userdata($author->ID);
    $id = $info->ID;
    $query = "SELECT userid, SUM(prizevalue) FROM wp_scloyalty_orders WHERE userid = '$id' GROUP BY userid"; 
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    echo $row['SUM(prizevalue)'];
    }
}

I would like to subtract the bottom function from the top. How would i go about doing this in another function so i can use it site wide?

Thanks, Dan

Upvotes: 0

Views: 88

Answers (2)

Borniet
Borniet

Reputation: 3546

Instead of

echo $row['SUM(prizevalue)'];

use

return $row['SUM(prizevalue)'];

this way you can assign the returned value from your functions to a new variable, and do whatever you want with that (for instance, calculations, or echo it) like this:

$newvar = total_prizes_value($author);
$newvar = $newvar + 5;

echo $newvar;

(will echo the result that your function would otherwise echo + 5)

Upvotes: 1

Mahmood Rehman
Mahmood Rehman

Reputation: 4331

$answer = total_prizes_value($author) - total_points();

Simply echo $answer, but use return statement instead of echo

Upvotes: 1

Related Questions