2011-02-16 75 views
0
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 = time() - (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"); 

問題:所有消息都是從數據庫中選擇的,而不僅僅是$ xdate和$ ydate之間的消息。'if語句'中的PHP time()未傳遞給MySql查詢?

但是,如果我在else語句中手動填充unix時間段(並且不要定義$ _SESSION ['periode']),則會從DB中選取正確的消息。這是怎麼回事??

回答

3
if($_SESSION['periode'] == 'Older'){ 
    $xdate = 0; 
    $ydate = time() - (7 * 24 * 60 * 60); 
    } 
else{ 
    $xdate = 0; 
    $ydate = time(); 
    } 

此代碼將始終設置$ ydate時間()和$ xdate爲0,如果$ _SESSION [「periode」]是不是「舊」,因爲這是其他相關的正上方,如果。 您需要使用ELSEIF,而不是如果

代碼必須

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(); 
} 
+0

我必須使用ELSEIF昨天/本週/舊的,當然!哇,我沒有看到...謝謝! – 2011-02-16 13:41:30