开发者

Modifying a PHP/Mysql timetable

开发者 https://www.devze.com 2023-02-27 00:38 出处:网络
I currently have this code: $rawsql2 = \"SELECT * FROM _erc_foffices n INNER JOIN _erc_openings o ON n.id = o.branch_id

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开发者_Python百科    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


Your PHP/MySQL here seems fundamentally flawed. Whatever you're doing with $day and $dotw can't be correct. Are you doing something to tie the day of the week with the entry in the database? If so your code isn't displaying it. Currently your code only looks at the current day of the week when you run the code. I see the issue you're describing, but in order to resolve that you'll need a way to indicate which times are associated with which days. Is this what you're trying to accomplish with opening_id? You never reference this table value in your code

EDIT This still feels incorrectly done since you're just assuming the rows are being passed in the correct order. The right way to do this would be to add rows to the table that correspond to the day of the week being processed so you can be sure your values are correct, but the quick fix would be to use a saftey bit that you flip on and off, and a variable to track when you're on a new row. Let me know if this doesn't make sense and I'll try to clarify

$saftey = 0;
$lastID = "";
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
if($lastID != $row["opening_id"])
{
    //New Row
    $lastID = $row["opening_id"];
    if($saftey = 0)
    {
        //Everything's fine, do all your processing

        //once that's done
        $saftey = 1;
    }
    else
    {
        //There was a first time, but no second time.  Close up here with <br>
    }
}
else
{
    //Not a new row, since this is the 2nd value, process normally and just in case
    $saftey = 0;
}


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 :/

0

精彩评论

暂无评论...
验证码 换一张
取 消