PHP time() in 'if statements' not passed to MySql query?
if($_SESSION['periode']){
if($_SESSION['periode'] == 'Today'){
$xdate = time() - (1 * 24 * 60 * 60);
$ydate = time();
}
if($_SESSION['periode'] == 'Yesterday'){
$xdate = time() - (2 * 24 * 60 * 60);
$ydate = time() - (1 * 24 * 60 * 60);
}
if($_SESSION['periode'] == 'This week'){
$xdate = ti开发者_开发知识库me() - (7 * 24 * 60 * 60);
$ydate = time() - (2 * 24 * 60 * 60);
}
if($_SESSION['periode'] == 'Older'){
$xdate = 0;
$ydate = time() - (7 * 24 * 60 * 60);
}
else{
$xdate = 0;
$ydate = time();
}
}
else {
$xdate = 0;
$ydate = time();
}
$tweets = mysql_query("SELECT * FROM messages WHERE content LIKE '%$search%'
AND content LIKE '%$region%'
AND time BETWEEN $xdate AND $ydate ORDER BY id DESC LIMIT 10");
The problem: all messages are selected from the database and not only those between $xdate and $ydate.
If I manually fill in a unix time period in the else statement however (and don't define $_SESSION['periode']) the correct messages are picked out of the DB. What's going on??
if($_SESSION['periode'] == 'Older'){
$xdate = 0;
$ydate = time() - (7 * 24 * 60 * 60);
}
else{
$xdate = 0;
$ydate = time();
}
This code will always set $ydate to time() and $xdate to 0 if $_SESSION['periode'] is not 'Older', because this else is related to just above if. You need to use elseif rather than if
code must be
if($_SESSION['periode']){
if($_SESSION['periode'] == 'Today'){
$xdate = time() - (1 * 24 * 60 * 60);
$ydate = time();
}
elseif($_SESSION['periode'] == 'Yesterday'){
$xdate = time() - (2 * 24 * 60 * 60);
$ydate = time() - (1 * 24 * 60 * 60);
}
elseif($_SESSION['periode'] == 'This week'){
$xdate = time() - (7 * 24 * 60 * 60);
$ydate = time() - (2 * 24 * 60 * 60);
}
elseif($_SESSION['periode'] == 'Older'){
$xdate = 0;
$ydate = time() - (7 * 24 * 60 * 60);
}
else{
$xdate = 0;
$ydate = time();
}
}
else {
$xdate = 0;
$ydate = time();
}
精彩评论