Page 1 of 1

counting a field

Posted: Mon Feb 20, 2006 4:58 am
by hame22
Hi all

I have records of transactions that take plac on my website held in a database.

What I want to do is display the transactions per product. So I am trying to add together each transaction per product.

I am having a little problem displaying this accumulated price.

My code is as follows;

Code: Select all

$result2  = admin_activity_transactions_query();
	
	
	while ($row2 = mysql_fetch_array($result2))
	{
		
		$product_id = $row2['product_id'];
		$trans_price = $row2['trans_price'];
		
		print '<p>'.$product_id.', '.$trans_price.'</p>';
}
and my query is

Code: Select all

function admin_activity_transactions_query()
{
	db_connect();
	
	$result = mysql_query("Select product_id, SUM(trans_price) from transactions group by product_id") or die(mysql_error());
	return $result;
}
at present all that is displayed is the product ID

any ideas as to why it is not working?

thanks in advance

Posted: Mon Feb 20, 2006 5:16 am
by JayBird
change your query to

Code: Select all

Select product_id, SUM(trans_price) as total_price from transactions group by product_id
then change

Code: Select all

$trans_price = $row2['trans_price'];
to

Code: Select all

$trans_price = $row2['total_price'];

Posted: Mon Feb 20, 2006 5:19 am
by hame22
thats the one, thanks!!

Posted: Mon Feb 20, 2006 5:30 am
by hame22
Check out using cronjobs on your server, these run scripts at particular times of the day

Posted: Mon Feb 20, 2006 5:57 am
by JayBird
hame22 wrote:Check out using cronjobs on your server, these run scripts at particular times of the day
huh? :?

Posted: Tue Feb 21, 2006 3:12 am
by hame22
sorry someone left a post then must have deleted it, regarding running scripts at different points of the day