Search code examples
phpmysqltimetable

Modifying a PHP/Mysql timetable


I currently have this code:

         $rawsql2 = "SELECT 
            *
            FROM 
                _erc_foffices n 
            INNER JOIN 
                _erc_openings o ON n.id = o.branch_id 
            INNER JOIN 
                _erc_openings_times t ON o.id = t.opening_id
            WHERE 
            (
                n.id = %d
            );";

            $sql2 = sprintf($rawsql2, mysql_real_escape_string($id));

            $result2 = mysql_query($sql2);

            /*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($result2) > 0) {

            $count=0;
            $day = 1;
            $timestamp = time();
            $dotw = date('w', $timestamp);
            $dotw = $dotw*2+1;

    echo "<div class='timetable'><p>";

while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {

    if ($day=='1') { if ($dotw == '1') {echo "<font color='$timetable_colour'>";} echo "<b>Sun - </b>";} 
    else if ($day=='3') { if ($dotw == '3') {echo "<font color='$timetable_colour'>";} echo "<b>Mon - </b>"; } 
    else if ($day=='5') { if ($dotw == '5') {echo "<font color='$timetable_colour'>";} echo "<b>Tue - </b>"; } 
    else if ($day=='7') { if ($dotw == '7') {echo "<font color='$timetable_colour'>";} echo "<b>Wed - </b>"; } 
    else if ($day=='9') { if ($dotw == '9') {echo "<font color='$timetable_colour'>";} echo "<b>Thu - </b>"; } 
    else if ($day=='11') { if ($dotw == '11') {echo "<font color='$timetable_colour'>"; } echo "<b>Fri - </b>"; } 
    else if ($day=='13') { if ($dotw == '13') {echo "<font color='$timetable_colour'>";} echo "<b>Sat - </b>"; } else { echo "";}

        if ($row["open"] == 0 && $row["close"] == 0) {

             if ($day % 2 == 1) {

                    echo "closed<br/></font>";

             }

        }

        else{

               echo "" . substr($row["open"], 0, -3) . "-" . substr($row["close"], 0, -3) . " &nbsp;&nbsp; ";

        if ($count % 2 !=0 ){ echo  "</font><br/>"; } }

               $count++;
               $day++;


                }
            } else {
                    echo "Error";
            }

which outputs a timetable like so:

Sun - closed
Mon - 08:30-12:30    13:30-16:30   
Tue - 08:30-12:30    13:30-23:59   
Wed - 08:30-12:30    12:30-12:30   
Thu - 08:30-12:30    13:30-16:30   
Fri - 08:30-12:30    13:30-16:30   
Sat - closed

The problem I've got now is that some of the embassies have only one opening time a day (e.g. Monday - 0900 - 17:00). With the system I have at the moment, I cannot insert just one time, because it will display the 0900 - 1700 in the monday column, and then the next value along for tuesday, but all in the same row.

Could someone tell me how I can alter my code to recognise that there is only one time for that day and line insert Tuesdays times on the correct line etc? Sorry if I haven't explained it very well, it's quite confusing!

Thanks for any help

Edit:

Mysql table layout:

id  opening_id  open    close
    1   3102    00:00:00    00:00:00
    2   3102    00:00:00    00:00:00
    3   3103    08:30:00    12:30:00
    4   3103    13:30:00    16:30:00
    5   3104    08:30:00    12:30:00
    6   3104    13:30:00    23:59:00
    7   3105    08:30:00    12:30:00
    8   3106    09:30:00    12:30:00
    9   3106    13:30:00    16:30:00
    10  3107    08:30:00    12:30:00
    11  3107    13:30:00    16:30:00
    12  3108    00:00:00    00:00:00
    13  3108    00:00:00    00:00:00

_erc_openings table

id  branch_id   dotw
1   1             1
2   1             2
3   1             3
4   1             4
5   1             5
6   1             6
7   1             7

Sun - closed
Mon - 08:30-12:30    13:30-16:30   
Tue - 08:30-12:30    13:30-23:59   
Wed - 08:30-12:30    09:30-12:30   
Thu - 13:30-16:30    08:30-12:30   
Fri - 13:30-16:30    Sat - closed

Solution

  • The answer that Daniel H found homself:

    I changed this:

         if ($row["open"] == 0 && $row["close"] == 0) {
         if ($day % 2 == 1) {
             echo "closed</font>";
         }
    }
    

    to this:

         if ($row["open"] == 0 && $row["close"] == 0) {
         if ($day % 2 == 1) {
             echo "closed</font>";
         }
             if ($day % 2 != 1) {
             echo "<br/></font>";
         }
    }
    

    and changed the second open/close times to 00:00:00 and it works how I wanted. I think I was over complicating things :/