2012-01-20 128 views
0

我有一個php MySql預訂日曆,它顯示了是否按日期預訂房間。問題是,它顯示了錯誤的信息。例如,如果我預訂了5-6,它應該標記爲第五個紅色,這意味着它在5日被預訂了。它顯示6日但6點,12點00分房間將是免費的。另一個例子:如果我預訂23-25,它顯示24,25爲預訂,但它應該顯示23-25,不知道問題在哪裏。php mySQL房間預訂日曆顯示預訂日期錯誤

下面是代碼:

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

} 
+0

我想$其中 和籤<= '$ _newdate' 和 '$ _newdate'<=結賬 這個SQL沒有得到正確的結果。 –

回答

1

它實際上是時間不匹配的問題。在分貝我設置日期時間作爲簽入和結帳,但我檢查sql與日期檢索數據沒有時間考慮。

現在還說:

$_newdate = $year.'-'.$month.'-'.$date . " " . "12:00:00"; 

工作就像一個魅力。 !!!!

+0

偉大的工作肯定 – humphrey

1

對不起,沒有太多的答案,但..

那麼我可以通過你的代碼,但我相信你可以自己解決這個問題,因爲它的 我相信只是邏輯錯誤。 你可以做的是打印從開始的所有結果變量,並通過它們調試 。這樣你就會知道哪條線給出了問題。

然後,您可以嘗試自己解決它或在此處詢問更精確的問題。

祝你好運! :)

+0

因爲我不善於SQL這就是爲什麼我沒有得到錯誤導致的東西。 –

+0

只需打印輸出值並獲取確切的錯誤行... 獲取確切的SQL或變量,這導致問題,我們可以從那裏移動 。 考慮一下你的第一個sql培訓:D P.S. - 只是一個問題..價值問題..日期被存儲在數據庫中是正確的?或其檢索問題? –

+0

它的檢索問題,如果5間客房預訂5日至6日在邏輯5應該預訂一晚,但它顯示第6作爲預訂第5空閒。 –