2012-01-20 10 views
0

私はphp MySql予約カレンダーを持っています。これは、予約されているかどうかを日付で表示します。問題は、間違った情報が表示されることです。たとえば、5〜6冊を予約した場合は、5桁目の赤色になり、5桁目に予約されたことを意味します。それは6番目に表示されますが、6時12:00:00までに部屋は無料になります。もう1つの例:私が23-25を予約すると、予約された24,25が表示されますが、問題がどこにあるのか分からない23-25が表示されます。ここでphp mySQL room予約カレンダーが間違った日付を表示している

は、コードは次のとおりです。

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

実際には時間のミスマッチです。 dbでは、私はチェックインとチェックアウトとして日付の時間を設定しましたが、時間を考慮する必要はありませんでした。

は今追加:

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

は魅力のように働いています。 !!!!

+0

は確かに偉大な仕事 – humphrey

1

、申し訳ありませんが答えの多くではないけど...

さて、私はあなたのコードを通過することができますが、私はあなたが そのため、この問題を自分で解決することができます確信しています私は信じているだけの論理エラーです。 あなたができることは、最初からすべての結果変数を印刷し、 をデバッグすることです。そうすれば、どのラインが問題になっているのかを知ることができます。

あなた自身で解決するか、ここでより正確な質問をすることができます。

Good Luck! :)

+0

私はsql thatsにうまくいかず、なぜ私は事を引き起こしているエラーを得ていないのですか? –

+0

出力値を出力して正確なエラーラインを取得してください。 問題を引き起こす正確なSQLまたは変数を取得して、そこから に移動できます。 これを最初のSQLトレーニングと見なします。D P.S. - 単なる質問です。価値の問題です。データベースに保存されている日付は正しいですか?またはその検索の問題? –

+0

その検索上の問題は、5泊5日から6日まで5泊で予約された場合、1泊分の予約が必要ですが、5日目の予約は6日目となります。 –

関連する問題