PHP Expire Post
Hello. I am seeking some help on job board script that I am working on. I am trying to use the below script to update the database if the current job is 30 days past the start date or post date. The code below is not working and if I change some things in the code, it will expire every post. Any help will be greatly appreciated. Thanks.
$resultx = mysql_query("SELECT * FROM jobs WHERE active='yes'")
or die(mysql_error());
while ($datax = mysql_fetch_array($resultx,MYSQL_NUM))
{
$id = $datax['id'];
$job_id = $datax['job_id'];
$sdate = $datax['sdate'];
$days = $data['days'];
$exp_date = $sdate + (60*60*24*30*3); //the number of seconds in 3 months
//$todays_date = date('Ymd', mktime (0, 0, 0, date ('m'), date ('d'), date ('Y')));
$now = time();
if($exp_date > $now) {
$update = "UPDATE jobs SET active='no' WHERE job_id = ".$job_id;
$results = mysql_query($update);
}
}
joshmac
Junior Poster in Training
72 posts since Apr 2008
Reputation Points: 16
Solved Threads: 4
change MYSQL_NUM to MYSQL_ASSOC. you are trying to use the names of the fields to get their respective values and its not working because you are getting a numeric array.
in the if statement at the end, change the greater than to less than.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
Thanks, but it did not work. It sets all of them to expire even though it is not past 30 days.
joshmac
Junior Poster in Training
72 posts since Apr 2008
Reputation Points: 16
Solved Threads: 4
echo out sdate. is it in unix time format. are the field names right. check stuff like that.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
Thanks but I think there is something wrong with my query. I updated my code to this:
$resultx = mysql_query("SELECT * FROM jobs WHERE active='yes'")
or die(mysql_error());
while ($datax = mysql_fetch_array($resultx,MYSQL_ASSOC))
{
$id = $datax['id'];
$job_id = $datax['job_id'];
$sdate = $datax['sdate'];
$days = $data['days'];
$exp_date = strtotime($sdate + (60*60*24*30*3)); //the number of seconds in 3 months
//$todays_date = date('Ymd', mktime (0, 0, 0, date ('m'), date ('d'), date ('Y')));
$now = time();
if($exp_date > $now) {
$update = "UPDATE jobs SET active='no' WHERE job_id = ".$job_id;
$results = mysql_query($update);
}
}
I added "strtotime" so that it would be equivalent to "time()". It works but it sets every posting in the database to inactive. So, it is not looking at just the one id of the post that is more than 30 days old. That is why I think there is a problem with my query, but I can't figure out the issue.
joshmac
Junior Poster in Training
72 posts since Apr 2008
Reputation Points: 16
Solved Threads: 4
dump all of your vars and make sure the info in the database is correct. i will give you an sql query as soon as i have more time.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
Thanks, I am going to sleep on it as well, so that I will have a clearer head. Thanks again for your help.
joshmac
Junior Poster in Training
72 posts since Apr 2008
Reputation Points: 16
Solved Threads: 4