counting a field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

counting a field

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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'];
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

thats the one, thanks!!
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

Check out using cronjobs on your server, these run scripts at particular times of the day
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

hame22 wrote:Check out using cronjobs on your server, these run scripts at particular times of the day
huh? :?
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

sorry someone left a post then must have deleted it, regarding running scripts at different points of the day
Post Reply