Jump to content

Sum of variable is wrong - place in function too


richrock

Recommended Posts

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

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.

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

$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');

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/>";
}

 

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.