selecting min and max bid for each item

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
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

selecting min and max bid for each item

Post by mccommunity »

I am doing a little piece for an auction company. What i am trying to do is query a bid table and pull all the items for a particular auction facility out of the bid table so item #1 might have 20 entries which are 20 different bids. So I am doing a select distinct just to grab the items that have bids on them. now I want to loop through each item grabbing all the records (bid information) and trying to grab the starting bid and the final maximum bid on each item. Here what I have so far and I am stuck, can anyone help me with this? Thanks a lot.


$strSQL = "select distinct item from bidlog where auction='$auction' and code='B' order by item ";
$result = pg_exec($con, $strSQL);
if(!$result)
{
print("<SCRIPT>alert('An unknown error occurred. The bidder information could not be retrieved, please try again. If it does not work contact AMS.')</SCRIPT>\n");
return "false";
}

$bid_rows = pg_numrows($result);
for($z=0;$z<$bid_rows;$z++)
{
$data = pg_fetch_row($result, $z);
$strSQL = "select * from bidlog where auction='$auction' and code='K' and item='$data[0]' order by item ";
$result = pg_exec($con, $strSQL);
IN THIS AREA I THINK I NEED TO DO AN ARRAY AND DO A MIN($ARRAY) AND MAX($ARRAY) BUT NOT REALLY SURE THE BEST WAY

}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you can get all the infos with one query
something like

Code: Select all

SELECT item, min(bid), max(bid), auction FROM bidlog WHERE code='K' GROUP BY item ORDER BY item
important is the GROUP BY clause. It allows you to use the aggregate functions max(<field>) and min(<field>) in that query
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

????

Post by itsmani1 »

wel me think volka is right.......................
Post Reply