Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Values together

Status
Not open for further replies.

7724

Programmer
Feb 25, 2005
28
GB
Hi,

I have two PHP commands which seperately add up two different sets of values individually.

Code:
<?php 

$sql = "SELECT COUNT(*) FROM game WHERE game.shirtone = '$secondname' OR game.shirttwo = '$secondname' OR game.shirtthree = '$secondname' OR game.shirtfour = '$secondname' OR game.shirtfive = '$secondname' OR game.shirtsix = '$secondname' OR game.shirtseven = '$secondname' OR game.shirteight = '$secondname' OR game.shirtnine = '$secondname' OR game.shirtten = '$secondname' OR game.shirteleven = '$secondname' "; 
$result = mysql_query($sql) or die(mysql_error()); 
echo mysql_result($result, 0); 
?>

Code:
<?php 

$sql = "SELECT COUNT(*) FROM game WHERE game.r1 = '$secondname' OR game.r2 = '$secondname' OR game.r3 = '$secondname' OR game.r4 = '$secondname' OR game.r5 = '$secondname' OR game.r6 = '$secondname' OR game.r7 = '$secondname' OR game.r8 = '$secondname' OR game.r9 = '$secondname' OR game.r10 = '$secondname' OR game.r11 = '$secondname' "; 
$result = mysql_query($sql) or die(mysql_error()); 
echo mysql_result($result, 0); 
?>

How would I go about adding the totals of these two sets of values together?

Thanks

Chris
 
Since the WHERE clauses of the two SQL statements are different it might not be possible to count the two simultaneously. Just assign the values to singleton variables and add them.
Code:
$sql = "SELECT COUNT(*) AS factor_a FROM game WHERE game.shirtone = '$secondname' OR game.shirttwo = '$secondname' OR game.shirtthree = '$secondname' OR game.shirtfour = '$secondname' OR game.shirtfive = '$secondname' OR game.shirtsix = '$secondname' OR game.shirtseven = '$secondname' OR game.shirteight = '$secondname' OR game.shirtnine = '$secondname' OR game.shirtten = '$secondname' OR game.shirteleven = '$secondname' ";
$result = mysql_query($sql) or die(mysql_error());
$row_a = mysql_fetch_assoc($result);
$total += $row['factor_a'];

# repeat with second query
$sql = "SELECT COUNT(*) AS factor_b FROM game WHERE game.r1 = '$secondname' OR game.r2 = '$secondname' OR game.r3 = '$secondname' OR game.r4 = '$secondname' OR game.r5 = '$secondname' OR game.r6 = '$secondname' OR game.r7 = '$secondname' OR game.r8 = '$secondname' OR game.r9 = '$secondname' OR game.r10 = '$secondname' OR game.r11 = '$secondname' ";
$result = mysql_query($sql) or die(mysql_error()); 
$row_b = mysql_fetch_assoc($result);
$total += $row['factor_b'];
# total is calculated
The code assigns two rows, you could re-use the same variable though. The factos are also named differently, they could be named the same. It's just for clarity in the example.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top