Jump to content

Room Allocation with PHP


aMOEBa

Recommended Posts

Hi, I need help with an algorithm to allocate rooms to people. I need to shuffle through a list of rooms to assign a room to a person and after the fourth person is assigned to the room, it should be out of the list(should be unavailable). I have not had any luck with PHP shuffling and sorting functions yet.

 

Any help (pointers, references, etc.) will greatly be appreciated.

Link to comment
Share on other sites

Does this function have to work over time (as people sign up?) or when you go to sort people into rooms, do you already know everyone who is attending?

 

Assuming you don't have to store all of this in a database as people sign up... it seems like you could do this using two arrays. You have one array of people who need rooms, and a 2D array like this:

 

array[total number of rooms][0-3]

 

You could go through each item in the people array, and randomly assign them to a room number between 0 and (total number of rooms -1). If array[randomly assigned room #][0] is available, you assign the person there. If array[randomly assigned room #][0] isn't available, but array[randomly assigned room #][1] is, you assign them there... and continue this process up through array[randomly assigned room #][3]. If array[randomly assigned room #][3] is taken, you then assign them to a new randomly generated room and start the process over.

 

To simplify this process, you could use a third array, which initially could hold the room numbers of all available rooms. When a room is filled, that particular room number is dropped from the available rooms array. When assigning a new person to a room, the script would only choose from available room numbers by selecting from the available rooms array.

 

Make sense? You shouldn't really need to deal with shuffling/sorting functions besides the use of rand(): http://php.net/manual/en/function.rand.php

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...