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 ?

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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 !

 

 

 

 

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.