Cflax Posted May 5, 2011 Share Posted May 5, 2011 did some searching on the internet and didnt find much. i have created an online classifieds website and want all created listings to expire and delete after a certain amount of days. honestly i have no idea where to start with this implementation. cookies were the only thing that even crossed my mind... any ideas? btw each listing is stored in a table called ob_listings Quote Link to comment https://forums.phpfreaks.com/topic/235608-database-item-expire-after-certain-amount-of-time/ Share on other sites More sharing options...
JKG Posted May 5, 2011 Share Posted May 5, 2011 cron job everyday to loop through all rows and delete if timestamp is x old? Quote Link to comment https://forums.phpfreaks.com/topic/235608-database-item-expire-after-certain-amount-of-time/#findComment-1210959 Share on other sites More sharing options...
The Little Guy Posted May 5, 2011 Share Posted May 5, 2011 I wouldn't loop through, that would/could take too much time. I would first select all the rows in the table that are a certain age, for example (10 = 10 days): delete from ob_listings where datediff(now(), ob_listing_date) > 10 So this would delete anything that is 11 or more days old. Personally, I would recommended not deleting the rows, I would just update a column that says whether the row is active or not, so I probably would do this: update ob_listings set is_active = 0 where datediff(now(), ob_listing_date) > 10 now when you want to get something from the database you just do this: select * ob_listings where is_active = 1 Quote Link to comment https://forums.phpfreaks.com/topic/235608-database-item-expire-after-certain-amount-of-time/#findComment-1211003 Share on other sites More sharing options...
perky416 Posted May 5, 2011 Share Posted May 5, 2011 Hi Cflax, I had the same problem as you, i didnt want to use a cron job so I created a piece of code similar to the following, and i use include() to include it in every page, that way every time a page is loaded it checks the database to see if a listing has expired, then deletes it as required. $query = mysql_query("SELECT * FROM ob_listings"); while ($row = mysql_fetch_assoc($query)) { mysql_query("DELETE FROM ob_listings WHERE NOW() > expiry_date"); } expiry_date is the table field that contains the expiry date. Hope it helps Quote Link to comment https://forums.phpfreaks.com/topic/235608-database-item-expire-after-certain-amount-of-time/#findComment-1211004 Share on other sites More sharing options...
perky416 Posted May 5, 2011 Share Posted May 5, 2011 I wouldn't loop through, that would/could take too much time. I would first select all the rows in the table that are a certain age, for example (10 = 10 days): delete from ob_listings where datediff(now(), ob_listing_date) > 10 So this would delete anything that is 11 or more days old. Very good point, i may use this myself. Quote Link to comment https://forums.phpfreaks.com/topic/235608-database-item-expire-after-certain-amount-of-time/#findComment-1211007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.