简体   繁体   中英

Booking system: Find room combinations based on guest count - PHP

I am preparing a booking system and solving a specific problem. I have the following data structure (room number/occupancy/ price):

$room[222][1] = 80;

$room[223][1] = 100; //Room ID 222, occupancy 1 guest, price €100
$room[223][2] = 200; //Room ID 222, occupancy 2 guest, price €200
$room[223][3] = 300; //...

$room[224][1] = 110;
$room[224][2] = 220;
$room[224][3] = 330;
...

I need to find the best (lowest price) combination of rooms for a specified number of guests.

For example: For the 4 guests is the best combination of room $room[222][1] and room $room[223][3]. The total price of the rooms is €380.

Can you help me?

First, I would make the rooms a more descriptive object so something like this: Second, I wouldn't store price per customer like that, rather focus on capacity and go from there

  $room = array();
  $room[222] = array(
    'capactiy' => '3',
    'price' => array(
      1 => '100', //Use a string, likely you'll have .99 or whatever
      2 => '200',
      3 => '300',
    ),
    'booked' => false,
  );

So you have 3 customers come in: Loop through available rooms, if the amount of people is less than capacity then cool, book it, else (say capacity is 2) then remove 2 from the amount of customers needing room, Then loop again and grab the next room

If you want to get complex, say they want to be right next to each other, take room 222 + 1, is it available? if no then 223 + 1, is it available? yes? great! book it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM