Jump to content

[SOLVED] adding a bunch of numbers from a query.


egiblock

Recommended Posts

i have a table.  colums y1,y2,y3,y4 ....

 

i want a total called YardsOut so i can display it on the site:

 

$query_CourseDetail = "select * from tbl_courses WHERE $id = tbl_courses.courseID";
$CourseDetail = mysql_query($query_CourseDetail, $golfscoring) or die(mysql_error());
$row_CourseDetail = mysql_fetch_assoc($CourseDetail);
$totalRows_CourseDetail = mysql_num_rows($CourseDetail);

$YardsOut = $row_CourseDetail['y1']+$row_CourseDetail['y2'];
echo $YardsOut;

 

is there a easier way to get the totals ?

 

you could select the sum directly:

 

SELECT (y1 + y2) AS yardsTotal FROM tbl_courses WHERE courseID = $id

 

after running the query and grabbing the result, 'yardsTotal' should contain the sum.

 

so i tried the following:

 

$sql = "SELECT (y1 + y2) AS yardsTotal FROM tbl_courses WHERE courseID = $id";
$yardstotal =  mysql_query($sql, $golfscoring);

echo $yardstotal; 

 

and got:

 

 

Resource id #536

 

 

 

 

sorry, but this is my first time with php and databases, especially mysql..  in the past i've always used coldfusion with access.

 

 

here's what i ended up with (golf scoring system...)

 

$YI = "SELECT (y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9) AS yardsIn FROM tbl_courses WHERE courseID = $id";

$YO  = "SELECT (y10 + y12 + y13 + y14 + y15 + y16 + y17 + y18) AS yardsOut FROM tbl_courses WHERE courseID = $id";

 

$yardsIn =  mysql_query($YI, $golfscoring);

$yardsOut =  mysql_query($YO, $golfscoring);

 

$Yin = mysql_fetch_assoc($yardsIn);

$Yout = mysql_fetch_assoc($yardsOut);

 

$yardsTotal = $Yin['yardsIn'] + $Yout['yardsOut'];

 

 

<?php echo $Yin['yardsIn']; ?>

<?php echo $Yout['yardsOut']; ?>

<?php echo $yardsTotal; ?>

 

 

thanks for the help people !

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.