tags:

views:

89

answers:

2

Hi Everyone... I am building an online hotel booking system.... Using php and mysql.... Users can search for and book hotels in a particular location .. The search criteria will be
1. City or country
2. Check - in and Check Out dates
3. Number of guests
My sql schema is as follows....

  1. Table : hotels
    hotel_id
    hotel_name
    country_id
    city_id

  2. Table : rooms
    room_id
    hotel_id
    room_name
    room_price
    max_guests_allowed
    no_total_rooms
    no_booked_rooms

  3. Table : reservations
    reservation_id
    room_id
    hotel_id
    check_in_date
    check_out_date

Query to get rooms free between two given dates and can accommodate persons equal to or more than the no. of persons given by the user :

SELECT rooms.room_id, 
       rooms.room_name, 
       rooms.max_guests_allowed, 
       rooms.room_price, 
       hotels.hotel_id, 
       hotels.hotel_name 
FROM   hotel,rooms 
WHERE rooms.id NOT IN (SELECT room_id 
                       FROM   reservations 
                       WHERE  check_in_date < '$arrivalDate' 
                       AND    check_out_date > '$leaveDate') 
AND   rooms.max_guests_allowed >= '$no_of_guests'; 

Now... I have to display the result like....

1. Hotel 1 
     Room 1     No. of Rooms Free     Price   Max. Guests Allowed 
     Room 2     No. of Rooms Free     Price   Max. Guests Allowed 
     Room 3     No. of Rooms Free     Price   Max. Guests Allowed

2. Hotel 2
     Room 1     No. of Rooms Free     Price   Max. Guests Allowed 
     Room 2     No. of Rooms Free     Price   Max. Guests Allowed 
     Room 3     No. of Rooms Free     Price   Max. Guests Allowed

etc.. etc...

How should i modify my query to group the rooms under the hotels like i have mentioned above...???? Thanks a lot in advance....

+1  A: 

I may be misunderstanding the question/query slightly, but couldn't you add:

ORDER BY hotels.hotel_name ASC,rooms.room_name ASC

to the end of the query to give you the results in hotel and then room order?

Edit to display in your original format, you could use some code similar to this. I've done it in PHP here as you haven't specified what language you're using:

$currentHotel = -1;
while ($row = mysql_fetch_array())
{
  if ($row["hotel_id"] != $currentHotel)
  {
    print "Hotel " . $row["hotel_id"] . "\n";
    $currentHotel = $row["hotel_id"];
  }

  // print your room details out here
  print "Room " . $row["room_name"];

  // ...
}
richsage
Yep.. But this will just order the hotels.. but will it group the rooms under their respective hotels..?
SpikETidE
@SpikeTidE this will order by the first part (hotel_name) ascending, then by room_name ascending, but keeping the order of hotel_name. So you should end up with what you've put in the question originally. Can you try it and see?
richsage
@richsage : Hi... Sorry for the late reply... Was out for the weekend... Your suggestion worked... It arranged each room under it's hotel... but still not in the way i wanted... i.e the format i've sown above.. Instead it shows like...Hotel 1 Room 1 Hotel 1 Room 2 Hotel 2 Room 1Hotel 2 Room 2etc... So.. trying to work with your idea to bring the format i wanted....
SpikETidE
@SpikETide I've added some sample (PHP) code to display roughly how you want it - does this help?
richsage
@richsage : That's what i was expecting, rich!!!! I've been tearing my hair with complex functions... Can't believe this could've been so simple... Thanks for helping me stop before i went completely bald..!! :) .... and yeah... i'm working with php...!!
SpikETidE
@SpikETide no problem :-)
richsage
A: 

http://www.bestchinahotel.com/