2011-04-12 34 views
0

我一直在使用這種代碼:PHP的開放時間和節假日使館

$rawsql = "SELECT 
* 
FROM 
    _erc_foffices n 
INNER JOIN 
    _erc_openings o ON n.id = o.branch_id AND o.dotw = DAYOFWEEK(CURRENT_DATE()) 
INNER JOIN 
    _erc_openings_times t ON o.id = t.opening_id 
WHERE 
(
    UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close)) 
) 
AND 
(
    n.id = %d 
) 
;"; 

$sql = sprintf($rawsql, mysql_real_escape_string($id)); 

$result = mysql_query($sql); 

/*These if & while statements decide whether or not the information should be displayed. If no results are returned from the query above then an alternative message is shown.*/ 

if(mysql_num_rows($result) > 0) { 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 

     echo "<div class='address'><p>" . $row["title"] . "<br/>"; 
     echo $row["address_1"] . "<br/> " . $row["address_2"] . "<br/> " . $row["address_3"] . "<br/> " . $row["address_4"] . "<br/> " . $row["address_5"] . "</p>"; 
     echo "<div class='buttons'><img src='http://localhost/erc/images/texttouser_button.png'/><br/><a href='" . $row["url"] . "' target='blank'><img src='http://localhost/erc/images/website_button.png'/></a></div></div>"; 
     echo "<div class='email'>" . $row["email"] . "</div>"; 
     $extra_notes = $row["extra"]; 

    } 
} else { 

     $embassy_closed = mysql_query("SELECT * FROM _erc_foffices WHERE id = '$embassy_id'"); 

     while($row = mysql_fetch_array($embassy_closed)) 
         { 

     echo "<div class='address'><p>" . $row["title"] . "<br/>"; 
     echo $row["address_1"] . "<br/> " . $row["address_2"] . "<br/> " . $row["address_3"] . "<br/> " . $row["address_4"] . "<br/> " . $row["address_5"] . " <font color='red'>The embassy is closed.</font></p>"; 
     echo "<div class='buttons'><img src='http://localhost/erc/images/texttouser_button.png'/><br/><a href='" . $row["url"] . "' target='blank'><img src='http://localhost/erc/images/website_button.png'/></a></div></div>"; 
     echo "<div class='email'>" . $row["email"] . "</div>"; 
     $extra_notes = $row["extra"]; 


         } 


} 

這需要開放時間從數據庫中使館並決定使館是否是當前打開的。如果不是,則顯示「使館關閉」的消息。現在我需要在公衆假期增加了,所以我改變了查詢,看起來像這樣:

$rawsql = "SELECT 
    * 
    FROM 
     _erc_foffices n 
    INNER JOIN 
     _erc_openings o ON n.id = o.branch_id AND o.dotw = DAYOFWEEK(CURRENT_DATE()) 
    INNER JOIN 
     _erc_openings_times t ON o.id = t.opening_id 
     LEFT JOIN 
    _erc_holidays h ON h.branch_id = n.id 
    WHERE 
    (
     UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close)) 
    ) 
    AND 
    (
     n.id = %d 
    ) 
     AND 
(
    UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) 
    NOT BETWEEN UNIX_TIMESTAMP(h.begins_at) AND UNIX_TIMESTAMP(h.ends_at) 
) 
     ;"; 

但這只是輸出地址/電子郵件等的兩倍。

有人能指出我做錯了嗎?

感謝所有幫助

編輯:我現在已經解決了這個問題,只是採用了一個2查詢的if/else循環。

回答

0

使用第二個查詢,如果/ else循環,它的工作正如我想。

0

我不會說自己是專家,你的連接看起來非常複雜,但根據我的經驗,如果你在查詢中得到重複,那麼你沒有正確分組,並且我看不到GROUP BY全部在你的查詢中。

希望有所幫助。

順便說一句......您從哪裏獲取公共假期數據?我現在正在尋找完全一樣的東西,這是我遇到這篇文章的方式。

+0

嗨Dave,我們的工作人員正在瀏覽所有大使館網站並手動收集數據。 – 2011-04-18 08:10:45