Search code examples
phpmysqltimetimetable

PHP opening times & embassy holidays


I've been using this code:

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


                      }


}

which takes the opening times for embassies from a database and decides whether or not the embassy is currently open. If it's not, a message saying 'The embassy is closed' is shown. Now I need to add in public holidays, so I have altered the query to look like this:

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

but this just outputs the address/email etc twice.

Could someone point out what I'm doing wrong please?

Thanks for any help

Edit: I have fixed this now, just used a 2nd query and if/else loop.


Solution

  • Used a 2nd query and if/else loop and it's working as I wanted.