PHP开放时间&使馆假期

时间:2011-04-12 09:32:10

标签: php mysql time timetable

我一直在使用这段代码:

 $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)
)
        ;";

但这只是输出地址/电子邮件等两次。

有人能指出我做错了吗?

感谢您的帮助

编辑:我现在已修复此问题,只使用了第二个查询和if / else循环。

2 个答案:

答案 0 :(得分:0)

我不会自称是专家,你的联接看起来很复杂,但根据我的经验,如果你在查询中得到重复,那么你没有正确分组,我根本看不到GROUP BY在你的查询中。

希望有所帮助。

顺便问一下......您从哪里获取公共假期数据?我正在寻找完全相同的东西,这就是我遇到这篇文章的方式。

答案 1 :(得分:0)

使用了第二个查询和if / else循环,它正如我想的那样工作。