richrock Posted September 24, 2008 Share Posted September 24, 2008 Hi, based on yesterday's great working out of calculating the sum of a single variable, I was aiming to use this on another page. I copied the code, placed it and tried the page. Checked the results with the spreadsheet, and I'm missing £2600. The total calculated by the spreadsheet is £406,260. The calculation below gives me £403,660. How?!? $getAuction = "SELECT * FROM jos_bid_auctions WHERE cat = ".$catnum.""; $rAuction = mysql_query($getAuction) or die(mysql_error()); $rAuc = (mysql_fetch_array($rAuction)); $auctitle = $rAuc['title']; $mytotal = 0; while ($r_auc = mysql_fetch_array($rAuction)) { $mytotal += $r_auc['est_low']; } echo $mytotal."<br />"; And I'm hoping to use this on many calculations, eg, total value, estimates (low and high) reserve price, etc. But being a complete n00b I can't figure out how to make it into a function for each one I need. Any help would be appreciated Rich Link to comment https://forums.phpfreaks.com/topic/125598-sum-of-variable-is-wrong-place-in-function-too/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2008 Share Posted September 24, 2008 I'm going to take a guess that the first row in your result set has a value of £2600 for 'est_low'. Your code is executing a mysql_fetch_array($rAuction) statement before your while() loop, but is only using the 'title' from the row that was fetched and is not making use of the 'est_low' value from that row. Link to comment https://forums.phpfreaks.com/topic/125598-sum-of-variable-is-wrong-place-in-function-too/#findComment-649367 Share on other sites More sharing options...
richrock Posted September 24, 2008 Author Share Posted September 24, 2008 Bizarrely enough I did a search for the value £2600 and there is nothing in the table with that value. Which makes it all the more weirder... :-\ I've updated the code, and now it's £400 above the spreadsheet's value. Argh! $getAuction = "SELECT * FROM jos_bid_auctions WHERE cat = ".$catnum.""; $rAuction = mysql_query($getAuction) or die(mysql_error()); //$rAuc = (mysql_fetch_array($rAuction)); //$auctitle = $rAuc['title']; //$auc_estlow = $rAuc['est_low']; $mytotal = 0; while ($r_auc = mysql_fetch_array($rAuction)) { $mytotal += $r_auc['est_low']; } echo $mytotal."<br />"; R Link to comment https://forums.phpfreaks.com/topic/125598-sum-of-variable-is-wrong-place-in-function-too/#findComment-649385 Share on other sites More sharing options...
Barand Posted September 24, 2008 Share Posted September 24, 2008 $getAuction = "SELECT SUM(est_low) as total FROM jos_bid_auctions WHERE cat = $catnum"; $rAuction = mysql_query($getAuction) or die(mysql_error()); $mytotal = mysql_result ($rAuction, 0, 'total'); Link to comment https://forums.phpfreaks.com/topic/125598-sum-of-variable-is-wrong-place-in-function-too/#findComment-649712 Share on other sites More sharing options...
richrock Posted September 25, 2008 Author Share Posted September 25, 2008 I had thought of this, but I'll need to write about 8 of them Thanks for the tips. I'll work on it a bit today. Client's changed his mind about the percentages and results.... Link to comment https://forums.phpfreaks.com/topic/125598-sum-of-variable-is-wrong-place-in-function-too/#findComment-650304 Share on other sites More sharing options...
Barand Posted September 25, 2008 Share Posted September 25, 2008 if those 8 are for different catnums ... $getAuction = "SELECT cat, SUM(est_low) as total FROM jos_bid_auctions GROUP BY cat"; $rAuction = mysql_query($getAuction) or die(mysql_error()); while (list($cat,$total) = mysql_fetch_row($rAuction)) { echo "$cat : $total <br/>"; } Link to comment https://forums.phpfreaks.com/topic/125598-sum-of-variable-is-wrong-place-in-function-too/#findComment-650325 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.