PHP/MySQL Auto Bidding System
mysql, php
Solution
I think you can use a condition tree, rather than a loop
This is an interesting question, from how I understand it this function will trigger whenever a new bid is made. What you want to do should be something like:
- Store the bid which the user is attempting to insert.
- Get the high bid of the current bidder (there should be no bids higher than this, as they would have been resolved by a prior iteration of this function).
Next:
/**
* $currentBidder = The current high bidder
* $highBid = The current high bidder's high bid
* $thisBidder = Bidder placing the new bid
* $thisBid = The bid that triggered the function
* $increment = The minimum bid increment
*/
function placeBid($currentBidder,$highBid,$thisBidder,$thisBid,$increment) {
if($thisBid > $highBid) {
// Insert $highBid as current bid, for $currentBidder
if($thisBid > $highBid + $increment) {
// Insert $thisBid into highbids table
// Insert $highBid + $increment as current bid, for $thisBidder
} else {
// Insert $thisBid as current bid, for $thisBidder
}
} else {
// Insert $thisBid as current bid for $thisBidder
if($highBid > $thisBid + $increment) {
// Insert $thisBid + $increment as current bid, for $currentBidder
} else {
// Insert $thisBid as current bid, for $currentBidder
}
}
}
Notes:
- In case the new bid is equal to the max bid, I have preferred on behalf of the current bidder over the newer bidder.
- In all cases, I have preferred the highest bid, even when this wouldn't be higher than the current max bid + increment.
Obviously you'd have to check if it's the first bid and if so, set the bid to the minimum asking value. You'd have to check that the bid is valid (greater than current bid + increment. I haven't included that code.
From what I can see, if you trigger the function every time a bid is made, you won't need a loop at all, just a condition tree.
Scenario:
Item Current Bids: A, 4000
Item Current Max: A, 4000
--> C bids 7500
Item Current Bids: A, 4000; C, 5000
Item Current Max: C, 7500
--> B bids 7500
Item Current Bids: A, 4000; C, 5000; B, 7500; C, 7500
Item Current Max: C, 7500
--> A bids 9000
Item Current Bids: A, 4000; C, 5000; B, 7000; C, 7500; A, 8500
Item Current Max: A, 9000
Problem
I'm in the process of writing a script for auto-bidding on an item. I think the easiest way to describe what I'm trying to do is to give you a scenario. Assuming $1,000 increments: ``` Asking Price: $1,000 Bidder 1: Max Bid of $4,000 -> High Bid: $1,000 Bidder 2: Max Bid of $3,000 -> High Bid: $3,000 -> [AUTO BIDDER 1] High Bid: $4,000 Bidder 3: Max Bid of $8,000 -> High Bid: $5,000 Bidder 4: Max Bid of $10,000 -> [AUTO BIDDER 3] High Bid: $8,000 -> High Bid: $9,000 ``` I'm trying to come up with a loop to go through them, but I'm not exactly sure how. I have a loop I came up with that would work for every bid, but I'd like to to skip making each $1,000 increment, and instead jump up the highest bid based on the max bids. I have two tables setup: bids and maxbids. This was the loop I thought of: - Insert New Bid - Start Loop - $high = get highest current bid from bids table - $next = get lowest maxbid where maxbid > $high from maxbids - if ($next >= ($high + increment) - Insert into bids - else // assume already have highest bid - break loop - end loop This would work, but as I said, this would keep inserting all $1,000 increments. I'd rather it work the way I showed above. Any suggestions?