php mySQL 房间预订日历显示预订时的错误日期

标签 php mysql calendar

我有一个 php MySql 预订日历,它显示房间是否按日期预订。问题是,它显示了错误的信息。例如,如果我预订了 5-6 号,则应该将第 5 号标记为红色,这意味着它是在 5 号预订的。显示为 6 号,但到 6 号 12:00:00 房间将空闲。再比如:我订的是23-25,显示的是24、25,但应该显示的是23-25,不知道问题出在哪里。

这是代码:

  function getAllRooms($date,$month,$year)
{
    global $db;
    $where = ' ';
    if ($_GET['room_type'] != '') {
        $where .= " HAVING room_type = '".$_GET['room_type']."'";
    }

    $sql = "SELECT room_type 
            FROM 
                room 
            GROUP BY 
                room_type 
            $where
            ";
    /*echo $sql;
    exit;*/
    $result = $db->Execute($sql);;
    $room = '';
    while (!$result->EOF) {
        $qs     = '?room_type='.$result->fields('room_type').'&month='.$month.'&year='.$year;
        $total  = get_total_rooms_by_type($result->fields('room_type'),$date,$month,$year);
        $room   .=
        '<div class="'.$result->fields('room_type').'"> 

            <a href="'.BASE_URL.'room_detail.php'.$qs.'">
                '.$result->fields('room_type').' ('.$total.')
            </a>
        </div>';
        $result->MoveNext();
    }
    $result->Close;
    return $room;
}
function get_total_rooms_by_type($room_type,$date,$month,$year)
{
    global $db;
    $_newdate   = $year.'-'.$month.'-'.$date;
    $sql            = "SELECT room_id FROM room where room_type = '$room_type' ";
    $room_results   = $db->Execute($sql);
    $room_ids       = array();
    while (!$room_results->EOF) {
        $room_ids[] = $room_results->fields('room_id');
        $room_results->MoveNext();
    }
    $room_results_str = implode(',',$room_ids);
    $where = ' where 1 = 1 ';
    $available = 1;
    if ($_GET['booking_status'] == '1') {
        $where .= ' and (booking_status = 1 or booking_status = 2)';
        $available = 0;
    } else if ($_GET['booking_status'] == '2') {
        $where .= ' and (booking_status = 2)';
        $available = 0;
    } 
    $sql = "select count(room_id) from bookings 
            $where 
                and checkin <= '$_newdate'
                and '$_newdate' <= checkout
                and room_id in ($room_results_str)
            ";
    if ($available == 0) {
        return $db->GetOne($sql);

    } else {
        return count($room_ids) - $db->GetOne($sql);
    }


}
function draw_calendar_room($month,$year){

    /* draw table */
    $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

    /* table headings */
    $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';


    /* days and weeks vars now ... */
    $running_day = date('w',mktime(0,0,0,$month,1,$year));
    $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week = 1;
    $day_counter = 0;
    $dates_array = array();

    /* row for week one */
    $calendar.= '<tr class="calendar-row">';

    /* print "blank" days until the first of the current week */
    for($x = 0; $x < $running_day; $x++):
        $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
        $days_in_this_week++;
    endfor;

    /* keep going with days.... */
    for($list_day = 1; $list_day <= $days_in_month; $list_day++):
        $calendar.= '<td class="calendar-day">';;

            $calendar.= '<div class="day-number" style=" padding:5px 5px 45px;background-color:'.getRoomColor($list_day,$month,$year).'">'.$list_day.'</div>';

            /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/


        $calendar.= '</td>';
        if($running_day == 6):
            $calendar.= '</tr>';
            if(($day_counter+1) != $days_in_month):
                $calendar.= '<tr class="calendar-row">';
            endif;
            $running_day = -1;
            $days_in_this_week = 0;
        endif;
        $days_in_this_week++; $running_day++; $day_counter++;
    endfor;

    /* finish the rest of the days in the week */
    if($days_in_this_week < 8):
        for($x = 1; $x <= (8 - $days_in_this_week); $x++):
            $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
        endfor;
    endif;

    /* final row */
    $calendar.= '</tr>';

    /* end the table */
    $calendar.= '</table>';

    /* all done, return result */
    return $calendar;
}
function getRoomColor($date,$month,$year)
{
    global $db;
    $where = ' ';
    if ($_GET['room_id'] != '') {
        $room_id    = $_GET['room_id'];

    } else {
        $sql = "SELECT room_id
                        FROM 
                            room 
                        WHERE
                            room_type = '".$_GET['room_type']."' order by room_number asc
                        ";
        $room_id = $db->GetOne($sql);;
    }
    $_newdate = "$year-$month-$date";
    $sql = "SELECT booking_status
            FROM 
                bookings 
            where 
                checkin <= '$_newdate'
                and 
                '$_newdate' <= checkout
                and 
                room_id = '$room_id'
            ";
    /*echo $sql;
    exit;*/
    $result = $db->GetOne($sql);;
    if ($result == 1) {
        return '#FF0';
    } else if ($result == 2) {
        return '#F00';
    } else {
        return '#64C733';
    }

} 

最佳答案

抱歉,答案不多,但是..

好吧,我可以检查你的代码,但我相信你可以自己解决这个问题,因为 我相信这只是逻辑错误。 您可以做的是从头开始打印所有结果变量 通过它们进行调试。这样您就会知道哪一行出现问题。

然后您可以尝试自己解决该问题或在此处提出更精确的问题。

祝你好运! :)

关于php mySQL 房间预订日历显示预订时的错误日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8946296/

相关文章:

java - 日历 : don't allow past timestamp

javascript - 如何将经过验证的 JavaScript 数据存入 MySQL 数据库

javascript - 如何在 javascript 中捕获 php session 变量而不刷新页面?

mysql - 如何更改 MySQL5 中的列名

mysql - 协助查询逻辑和操作顺序

jquery - 将信息添加到 jquery datepicker 日历中的日期数字

php - 无法通过 PDO 连接到 AWS RDS

PHPUnit 错误 "undefined index : HTTP_HOST"

php - 一个父行,另一个表中的多个子行。如何将它们全部排成一排?

java - 1582年10月15日之前的日历到日期的转换。公历到儒略历的切换