0

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....

2
  • Are your tables complete? Is the only relationship between hotel and room in the reservations table? Can I reserve the same room in a different hotel?
    – Peter Lang
    Dec 27, 2009 at 9:41
  • @Peter : You are right, Peter.... i've left out one field : hotel_id telling under which hotel the room is present.. i'll edit the post..... Thank You
    – SpikETidE
    Dec 28, 2009 at 4:31

1 Answer 1

1

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"];

  // ...
}
5
  • Yep.. But this will just order the hotels.. but will it group the rooms under their respective hotels..?
    – SpikETidE
    Dec 26, 2009 at 12:14
  • @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
    Dec 26, 2009 at 13:07
  • @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 1 Hotel 2 Room 2 etc... So.. trying to work with your idea to bring the format i wanted....
    – SpikETidE
    Dec 28, 2009 at 6:44
  • 1
    @SpikETide I've added some sample (PHP) code to display roughly how you want it - does this help?
    – richsage
    Dec 28, 2009 at 9:27
  • @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
    Dec 28, 2009 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.