egiblock Posted June 19, 2009 Share Posted June 19, 2009 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 https://forums.phpfreaks.com/topic/162939-solved-adding-a-bunch-of-numbers-from-a-query/ Share on other sites More sharing options...
akitchin Posted June 19, 2009 Share Posted June 19, 2009 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. Link to comment https://forums.phpfreaks.com/topic/162939-solved-adding-a-bunch-of-numbers-from-a-query/#findComment-859727 Share on other sites More sharing options...
egiblock Posted June 20, 2009 Author Share Posted June 20, 2009 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 https://forums.phpfreaks.com/topic/162939-solved-adding-a-bunch-of-numbers-from-a-query/#findComment-860010 Share on other sites More sharing options...
Ken2k7 Posted June 20, 2009 Share Posted June 20, 2009 <?php $result = mysql_fetch_assoc($yardstotal); echo $result['yardsTotal']; Link to comment https://forums.phpfreaks.com/topic/162939-solved-adding-a-bunch-of-numbers-from-a-query/#findComment-860014 Share on other sites More sharing options...
egiblock Posted June 21, 2009 Author Share Posted June 21, 2009 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 https://forums.phpfreaks.com/topic/162939-solved-adding-a-bunch-of-numbers-from-a-query/#findComment-860505 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.