I am building a simple auction application. I need auctions to start and end at certain times. Should the page that displays the open auctions just run a query to find all auctions where current time is after the start time and before the end time? Or would it bet be开发者_开发百科tter to have a script that sets a "active" column to True? If this is the case would I have to have some type of cronjob setup?
No cron and no "active" column is required. Just list auctions using something like:
select *
from auctions
where
start < now()
and end > now()
When user is placing a bid use the following:
update auctions set
bid = $bid,
highest_bidder = $bidding_user_id
where
id = $this_auction_id
and start < now()
and end > now()
and bid < $bid
Then check if query has affected a row. If yes - bid is successful, current user is highest bidder. If no - bid is too low or auction has finished. You can figure out that later by fetching auction row again and checking bidder id.
Just set a "close_time" field in the mysql table with your auction. When you add the row, populate that field with a time() + whatever
to set the close_time to represent some point in the future.
Then setup a script which runs a simple query:
'UPDATE auctions_table SET active = FALSE WHERE active = TRUE AND close_time <= UNIX_TIMESTAMP()'
Save that query to a php page called something like close_active_auctions.php
and set that up on a cron for once every 10 seconds or so (or however often you need).
精彩评论