修改PHP / Mysql时间表

时间:2011-04-20 07:53:31

标签: php mysql timetable

我目前有这段代码:

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

输出如下的时间表:

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

我现在遇到的问题是,一些大使馆每天只有一个开放时间(例如星期一 - 0900 - 17:00)。使用我目前的系统,我不能只插入一次,因为它将在星期一列中显示0900 - 1700,然后在星期二显示下一个值,但所有都在同一行。

有人可以告诉我如何更改我的代码,以确认当天只有一次时间和行周二插入正确的行等吗?对不起,如果我没有很好地解释它,那就太混乱了!

感谢您的帮助

编辑:

Mysql表格布局:

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

2 个答案:

答案 0 :(得分:1)

你的PHP / MySQL似乎存在根本性的缺陷。无论你用$ day和$ dotw做什么都不正确。您是否正在做一些事情来将星期几与数据库中的条目联系起来?如果是这样,您的代码不会显示它。目前,您的代码仅查看运行代码时的当前星期几。我看到了您正在描述的问题,但为了解决这个问题,您需要一种方法来指示哪些时间与哪些天相关联。这是你想用opens_id完成的吗?您永远不会在代码中引用此表值

修改 这仍然感觉不正确,因为您只是假设行以正确的顺序传递。正确的方法是在表格中添加与正在处理的星期几相对应的行,这样您就可以确定您的值是正确的,但快速修复将使用您打开的安全位和关闭,以及在新行上跟踪的变量。如果这没有意义,请告诉我,我会尝试澄清

$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;
}

答案 1 :(得分:0)

Daniel H找到的答案:

我改变了这个:

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

到此:

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

并将第二个打开/关闭时间更改为00:00:00,它的工作原理我想要的。我想我过于复杂化了:/