Languages :: PHP :: How to achieve this in php? |
|||
| By: fkeessen |
Date: 15/07/2003 00:00:00 |
Points: 70 | Status: Answered Quality : Excellent |
|
Hi All, Can you please help me with the following: I'm writing an hotel reservation system in php; everything goes o.k. but this 'challenge' is giving me a lot of trouble: background: I've got 2 hotel rooms: 1 hotel room is a single room (occupation: 1 adult and 1 child). 1 hotel room is a double room (occupation: 2 adult and 2 childeren). 1 hotel room is a twin room (occupation 3 adults and 2 childeren). I have the following information retrieved already: How many adults their are and how many childeren; THE CHALLENGE: Scenario 1 So when i have 5 adults and 3 childeren the system must give as output: 1 x double room 1 x twin room Scenario 2: 6 adults and 4 childeren output: 3 x double rooms OR 2 x twin rooms Hope someone can push me in the right direction with some code... |
|||
| By: VGR | Date: 15/07/2003 04:25:00 | Type : Comment |
|
| well, given you've only three types (or more, but anyway a very limited number) of room types, you can do it with a very simple recursive function. Give me 10 minutes |
|||
| By: VGR | Date: 15/07/2003 17:00:00 | Type : Comment |
|
| --------------------results : 1 twin room and 1 double room 2 twin rooms --------------------code : <?php function whatIsBestChoice($nbparents,$nbchildren) { // : String if ($nbparents>=3) { $nb=floor($nbparents/3); $type='twin'; $restp=$nbparents-3*$nb; $restc=$nbchildren-$nb*2; } elseif ($nbparents>=2) { $nb=floor($nbparents/2); $type='double'; $restp=$nbparents-2*$nb; $restc=$nbchildren-$nb*2; } else { $nb=$nbparents; $type='single'; $restp=$nbparents-$nb; $restc=$nbchildren-$nb; } $result="$nb $type room".(($nb>1)?'s':''); if (($restp+$restc)>0) $result.=' and '.whatIsBestChoice($restp,$restc); return($result); } // whatIsBestChoice String Recursive Function echo whatIsBestChoice(5,3).' '; echo whatIsBestChoice(6,4).' '; ?> |
|||
| By: fkeessen | Date: 16/07/2003 19:56:00 | Type : Comment |
|
| Hi, Thanks for that FAST (i mean REALLY FAST) reaction!.. We're almost there.. But i've got 2 extra Scenario's Scenario 1 When you have 2 chideren, there can only be 2 adults in the room (now you can have 3 adults and 2 childeren).. THis was the wrong input I gave to you: Scenario 2: 6 adults and 4 childeren output: 3 x double rooms OR !!!!! 2 x twin rooms <- !!!!! Scenario 2 What i wanted was: When i have 3 adults it must be: 1 twinroom OR 1 double room 1 single room When i have 5 adults it must be: 2 double rooms 1 single room OR 1 double room 1 twin room Thanks again!!! Regards, Frank |
|||
| By: ispaleny | Date: 16/07/2003 20:08:00 | Type : Comment |
|
| Hi Frank, I solved it by randomly sorted recordsets in DB. 1. Find free rooms 2. In a limited number of loops: 2a. Randomly sort the free rooms and return the smallest resultset matching the condition from the top 2b. Store the resultset 2c. Next loop 3. Return the best matching resultset(s) 4. Results are non-deterministic !!!!! Best regards, Ivo Spaleny. --MSSQL code create table dbo.RoomTypes( IdType int identity(1,1) primary key ,CntAdults int not null ,CntChildr int not null ,check (CntAdults>0 and CntChildr>0) ) create table dbo.Rooms( IdRoom int identity(1,1) primary key ,Type int not null references dbo.RoomTypes(IdType) ) create table dbo.Reservations( IdReserv int identity(1,1) primary key ,Room int not null references dbo.Rooms(IdRoom) ,DateFrom datetime not null ,DateTill datetime not null ,check (DateFrom<DateTill) ) GO insert RoomTypes(CntAdults,CntChildr) values (1,1) insert RoomTypes(CntAdults,CntChildr) values (2,2) insert RoomTypes(CntAdults,CntChildr) values (3,2) insert Rooms(Type) values (1) insert Rooms(Type) values (1) insert Rooms(Type) values (2) insert Rooms(Type) values (2) insert Rooms(Type) values (3) insert Rooms(Type) values (3) GO --CREATE ALTER PROCEDURE spGetRooms(@DateFrom datetime,@DateTill datetime,@CntAdults int,@CntChildr int,@Estimates int) as BEGIN set nocount on declare @Estimate int set @Estimate=1 declare @FitFn_Cnt int set @FitFn_Cnt=9000000 declare @IdNew int declare @Test_FitFn_Cnt int create table #Result(Run int not null,FitFn int not null,IdRoom int not null) select cast(r.IdRoom as int) as IdRoom into #Source from dbo.Rooms r where not exists( select r.IdRoom from dbo.Reservations res where r.IdRoom=res.Room and ( @DateFrom not between res.DateFrom and res.DateTill ) and ( @DateTill not between res.DateFrom and res.DateTill ) and not ( @DateFrom < res.DateFrom and res.DateTill < @DateTill ) ) select IdRoom,IdNew=identity(int,1,1) into #Try from #Source where 1<>1 while @Estimate<=@Estimates begin delete #Try insert #Try(IdRoom) select IdRoom from #Source order by NEWID() set @IdNew= ( select top 1 r1.IdNew from #Try r1 join #Try r2 on r1.IdNew>=r2.IdNew join dbo.Rooms r on r2.IdRoom=r.IdRoom join dbo.RoomTypes rt on r.Type=rt.IdType group by r1.IdNew having sum(rt.CntAdults)>=@CntAdults and sum(rt.CntChildr)>=@CntChildr order by r1.IdNew ) select @Test_FitFn_Cnt=nullif(count(IdRoom),0) from #Try where IdNew<=@IdNew if @FitFn_Cnt >= @Test_FitFn_Cnt begin set @FitFn_Cnt = @Test_FitFn_Cnt insert #Result(Run,FitFn,IdRoom) select @Estimate,@FitFn_Cnt,IdRoom from #Try where IdNew<=@IdNew end set @Estimate = @Estimate + 1 end --Remove duplicites delete #Result from ( select distinct r1.Run from #Result r1 join #Result r2 on r1.IdRoom=r2.IdRoom and r1.Run>r2.Run where r1.FitFn=@FitFn_Cnt and r2.FitFn=@FitFn_Cnt group by r1.Run, r2.Run having count(*)=(select count(*) from #Result r3 where r1.Run=r3.Run) ) as X where X.Run=#Result.Run --Return result All --select Run,IdRoom from #Result where FitFn=@FitFn_Cnt --Return result One select Run,IdRoom from #Result where Run=(select top 1 Run from #Result where FitFn=@FitFn_Cnt) END GO declare @DateFrom datetime set @DateFrom = '20030716 15:30' declare @DateTill datetime set @DateTill = '20030717 14:00' declare @CntAdults int set @CntAdults = 6 declare @CntChildr int set @CntChildr = 4 declare @Estimates int set @Estimates=100 exec spGetRooms @DateFrom, @DateTill, @CntAdults, @CntChildr, @Estimates |
|||
| By: VGR | Date: 16/07/2003 20:14:00 | Type : Comment |
|
| well fkeessen , if I've time, I will try to adapt to your **new** requirements, but I don't promiss anything and in the meantime I think I earned my points :D (kidding) |
|||
| By: fkeessen | Date: 16/07/2003 21:13:00 | Type : Comment |
|
| Hi VGR, I think so ;> !! But can you please find time to see if you are able to implement those??? Thanks for your time! Frank |
|||
| By: VGR | Date: 16/07/2003 21:31:00 | Type : Comment |
|
| van you explain again (more clearly) where is the problem in the first description ? I didn't get the "no more than 2 adults if 2 children" rule Mreover, my scriptlet is sooooo simple that it can not (and won't) display the "OR ... second choice" part. It'll try to maximize the use of the rooms in the descending order. |
|||
| By: fkeessen | Date: 17/07/2003 22:09:00 | Type : Comment |
|
| Hi ispaleny, I have finnaly test your script.. It works the way I want.. Only problem is that the i can't use the Stored procedure because i'm using MYSQL. Mysql doesn't support stored procedures.. And the problem is i don't know how to convert the Stored procedure to a normal Select statement or function (Any idea).. Can someone help to see if i can use this script of ispaleny in mysql (+php)?? Regards, Frank |
|||
| By: fkeessen | Date: 17/07/2003 23:57:00 | Type : Comment |
|
| VGR, Hope i can explain it better for you: Scenario We simplify this one 1 hotel room is a single room (occupation: 1 adult and 1 child). 1 hotel room is a double room (occupation: 2 adult and 2 childeren) OR 1 hotel room is a twin room (occupation 3 adults without childeren). Twin room has left... So; When i have 4 adults; system must give as output 2 x double room When i have 5 adults; system must say 2 double rooms and 1 single room OR 2 double rooms When i have 5 adults and 4 childeren system must say 2 double rooms and 1 single room When i have 6 adults and 4 childeren system must say 3 double rooms When i have 3 adults and 2 childeren system must say 1 double room and 1 single room When i have 3 adults system must say 1 double room OR 1 double room + 1 single room I hope something is clearer now! Regards, Frank |
|||
| By: VGR | Date: 18/07/2003 00:55:00 | Type : Comment |
|
| if you prefer the dirty solution exposed in the stored proc above, I won't lose my time trying to adapt a script that had filled in the initial requirements from the "Question" :D |
|||
| By: fkeessen | Date: 18/07/2003 19:05:00 | Type : Comment |
|
| Hi Vgr, I'm preffering your solution... Yesterday i've tested the solution of ispaleny... But it's so difficult... So can you please work it out for me?? Regards, Frank |
|||
| By: VGR | Date: 18/07/2003 19:39:00 | Type : Comment |
|
| ok |
|||
| By: VGR | Date: 18/07/2003 19:48:00 | Type : Answer |
|
| 4 adults 0 children : 2 double rooms 5 adults 0 children : 2 double rooms 5 adults 4 children : 2 double rooms and 1 single room 6 adults 4 children : 3 double rooms 3 adults 2 children : 1 double room and 1 single room 3 adults 0 children : 1 double room I just commented out the first test, as you seem to wish to abandon the "twin room" concept it's not far from what you required... <?php function whatIsBestChoice($nbparents,$nbchildren) { // : String // if ($nbparents>=3) { $nb=floor($nbparents/3); $type='twin'; $restp=$nbparents-3*$nb; $restc=$nbchildren-$nb*2; } // else if ($nbparents>=2) { $nb=floor($nbparents/2); $type='double'; $restp=$nbparents-2*$nb; $restc=$nbchildren-$nb*2; } else { $nb=$nbparents; $type='single'; $restp=$nbparents-$nb; $restc=$nbchildren-$nb; } $result="$nb $type room".(($nb>1)?'s':''); if (($restp+$restc)>0) $result.=' and '.whatIsBestChoice($restp,$restc); return($result); } // whatIsBestChoice String Recursive Function echo '4 adults 0 children : '.whatIsBestChoice(4,0).' '; echo '5 adults 0 children : '.whatIsBestChoice(5,0).' '; echo '5 adults 4 children : '.whatIsBestChoice(5,4).' '; echo '6 adults 4 children : '.whatIsBestChoice(6,4).' '; echo '3 adults 2 children : '.whatIsBestChoice(3,2).' '; echo '3 adults 0 children : '.whatIsBestChoice(3,0).' '; ?> |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








