PDA

View Full Version : if-then-else or switch?


student101
04-11-2008, 06:25 AM
Should or could I use the "if-then-else" or switch?
What I would like to is run a query based on the date, say 30 days.

You upload an image and allow to display for 30 days before it's automatically deleted; based on the query that checks the date.


//get timestamp for past/future date I want
$pf_time = strtotime("-30 days");
//format the date using the timestamp generated
$pf_date = date("Y-m-d H:i:s", $pf_time);

$sql = "SELECT * FROM tbmobiledata";
$result = mysql_query($sql);
while($row_rsmobiles = mysql_fetch_array($result)){

$when = strtotime($row_rsmobiles['dateadded']); // 2008-04-10 18:04:44
$when_date = date("Y-m-d H:i:s", $when);

$today = date("Y-m-d H:i:s");

if($when_date == $pf_date){
echo $when_date ." equal to ". $pf_date ;
//Send notify email before delete query

} elseif($when_date != $pf_date){
echo $when_date ." not equal to ". $pf_date ;
//Send notify email before delete query

} elseif($when_date < $pf_date){
echo $when_date ." less than ". $pf_date ;
//Send notify email before delete query

} elseif($when_date > $pf_date){
echo $when_date ." greater than ". $pf_date ;
//Send notify email before delete query

}else {
echo "You are getting tired, time for coffee -, wonder if this could go in a switch?";
}


How to run it firstly and secondly is it possible to change this to a switch?

Cheers

davidj
04-11-2008, 07:51 AM
you can only use a switch when wanting to process on a match of a string

you cant use (<>) operators so a switch wont do the job

student101
04-11-2008, 07:58 AM
Cool got it, No switch
I can only use these:
== equals to
!= not equals to

So how to delete records older than x days (30/60/90/365)?

student101
04-11-2008, 08:28 AM
Would this work?

$q_delete = "DELETE FROM table WHERE ((TO_DAYS(CURDATE()) - TO_DAYS(Date_created)) > 30)";

student101
04-11-2008, 09:58 AM
you can only use a switch when wanting to process on a match of a string
you cant use (<>) operators so a switch wont do the job

Got this far:


<?php
$pf_time = strtotime("-30 days");
$pf_date = date("Y-m-d H:i:s", $pf_time);

do {

$dateadded = $row_rsmobiles['dateadded'];
$emailid2 = $row_rsmobiles['emailid'];

if ($row_rsmobiles['dateadded'] > $pf_date){

echo $emailid2 . " ". $dateadded . " " . " less than 30 ". $pf_date . " KEEP IT!!! <br />";

}else{

echo $emailid2 . " ". $dateadded . " " . " greater than 30 " . $pf_date . " DELETE IT!!! <br />";
}

} while ($row_rsmobiles = mysql_fetch_assoc($rsmobiles));

?>


The result is not great:

The first $emailid2 does not show up?
Is it perhaps the do { part?

Any ideas?

Cheers

davidj
04-11-2008, 11:44 AM
try a while loop


while ($row_rsmobiles = mysql_fetch_assoc($rsmobiles)){

$dateadded = $row_rsmobiles['dateadded'];
$emailid2 = $row_rsmobiles['emailid'];

if ($row_rsmobiles['dateadded'] > $pf_date){

echo $emailid2 . " ". $dateadded . " " . " less than 30 ". $pf_date . " KEEP IT!!! <br />";

}else{

echo $emailid2 . " ". $dateadded . " " . " greater than 30 " . $pf_date . " DELETE IT!!! <br />";
}

}

student101
04-11-2008, 12:14 PM
Interesting:

Replacing the do with the while only shows two of the three records?
//do {
while ($row_rsmobiles = mysql_fetch_assoc($rsmobiles)){

There is a defintely a difference but I don't think each record gets affected, odd?
user1 2008-03-11 12:00:00 greater than 30 2008-03-12 15:16:11 DELETE IT!!!
user2 2008-04-11 12:00:00 less than 30 2008-03-12 15:16:11 KEEP IT!!!


Cheers

student101
04-11-2008, 01:22 PM
Working,
There were too many SQL queries on the same page; :-?

It works with do or while :)

Thank you.