Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,156,713 members, 7,831,238 topics. Date: Friday, 17 May 2024 at 03:56 PM

Idealws's Posts

Nairaland Forum / Idealws's Profile / Idealws's Posts

(1) (of 1 pages)

Webmasters / Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by Idealws: 10:07am On May 28, 2009
Hello,

Well this is my first post so i hope this is helpful. I noticed the $x variable is being incremented
by one regardless of if it is a expired add or not. This is fixable by removing the ++$x; and putitng
it in the if() statement like below:


<?php
//select information from client where client is due for expiry
if ($expiry_date == $todays_date){
    $query = '' . 'UPDATE listings SET status=\'Expired\' WHERE id=\'' . $id . '\'';
     $result2 = mysql_query ($query);
     $x++;
}
?>


I added a field to my database called numberdays to set the expire time to a different
one depending on the add. Here is my finished code in case it helps anyone.


<?php
// Lets check to see if any featured ads are expired
$resultx = mysql_query("SELECT * FROM listings WHERE status='Active'"wink;
$x = 0;
while ($datax = mysql_fetch_array ($resultx)) {
$id = $datax['id'];
$days_to_expire = $datax['numberdays'];

//should be in the format of "Ymd"
$sdate = $datax['datestarted'] ;

//todays date
$todays_date = date('Ymd', mktime (0, 0, 0, date ('m'), date ('d'), date ('Y')));

$timeStamp = strtotime("$sdate"wink;
$timeStamp += 24 * 60 * 60 * $days_to_expire ; // (add expirydate days)
$expiry_date = date("Ymd", $timeStamp);

//select information from client where client is due for expiry
if ($expiry_date == $todays_date){
    $query = '' . 'UPDATE listings SET status=\'Expired\' WHERE id=\'' . $id . '\'';
        $result2 = mysql_query ($query);
        $x++;
}
}
$report = '' . $x . ' Number of Account Expired';
?>


Here is my table for this code:

CREATE TABLE IF NOT EXISTS `listings` (
  `id` bigint(20) NOT NULL auto_increment,
  `vid` bigint(20) NOT NULL,
  `sectionid` varchar(20) NOT NULL,
  `datestarted` varchar(cool NOT NULL,
  `numberdays` tinyint(4) NOT NULL,
  `status` varchar(25) NOT NULL,
  PRIMARY KEY  (`id`)
);


I keep my listings in a different table but you get the idea. This way I can have ads
that list for 30,45,90 days if I want.

Hope this helps.

Regards,
Ray

(1) (of 1 pages)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 10
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.