By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,807 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,807 IT Pros & Developers. It's quick & easy.

foraech loop inside a for loop

P: 30

I got stuck again....

In my Hotel Reservation script all reservations are saved in a flat file which name is the room number. (example: Data/room1-101.dat).

Reservations are saved in the following manner.

Expand|Select|Wrap|Line Numbers
  1. 3
  2. John Smith|||||||||Day:,Eve:|25|4|2009|25|4|2009|11|30
  3. James Taylor|||||||||Day:,Eve:|26|4|2009|26|4|2009|11|30
  4. Peter Willam|||||||||Day:,Eve:|10|4|2009|20|4|2009|11|30
This means there are three reservations:
First: starts 4/25/2009 ends same day (it is reservation for one day)
Second: starts 4/26/2009 ends same day (it is reservation for one day)
Third: starts 4/10/2009 and ends 4/20/2009 (reservation for 10 days).

I have a script that is supposed to show the reserved rooms in a table representing any given month named: Reserved_Rooms.cgi. Free rooms are green color. Reserved rooms are different colors.

My problem is that I cannot display all the resevations saved in Data/room1-101.dat . If you are able to open in a browser the script attached (Reserved_Rooms.cgi), and you look at the table it only shows the reservation in the forth line.

Room 106 has the same reservations as 101 but I shifted the order. Again it only shows the reservation on 4th line.

in Reserved_Rooms.cgi the first Blue column to the left shows all the room numbers followed to the right with column digits representing the day of the months (up to 31).

The coloring of rooms is determined by a snippet of code in Reserved_Rooms.cgi (lines 328 through 340) as follows:

Expand|Select|Wrap|Line Numbers
  1.      for($ca=0;$ca<=$Rooms_Reserved;$ca++)
  2. {
  3. ($Client_Name,$O1,$O2,$O3,$O4,$O5,$O6,$O7,$O8,$O9,$Start_Date,$Start_Month,$Start_Year,$End_Date,$End_Month,$End_Year,$Check_Out_Hour,$Check_Out_Min)=split (/\|/,$data[$ca]);    
  4. if($is == $sday) {$width ='30';}else{$width ='10';}
  5. if(($is < $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#9A9A9A';$Checkbox = '';}
  6. elsif(($is == $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#707070';$Checkbox = '';}
  7. elsif(($is > $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#FF0000';$Checkbox = '';}
  8. else{$bgcolor = '#00FF00';}
  9. if(($bgcolor eq '#00FF00')&&($is >= $sday))
  10. {$Checkbox = "<input type=checkbox name=\"RoomNum\" value=\"$is-$i-$rooms[$m-1]\">";}## (date-type-rumnum ###
  11. elsif($is < $sday){$Checkbox = '';}
  13. }# END OF for($ca=0;$ca<=$Rooms_Reserved;$ca++)
I have tried inserting a foreach loop inside the for loop, in order to display each line of the flat file Data/room1-101.dat, in the following manner but it didnt work:

Expand|Select|Wrap|Line Numbers
  1.    for($ca=0;$ca<=$Rooms_Reserved;$ca++)
  2. {
  3. push(@All_Reservations, $data[$ca]);
  4. foreach $line (@All_Reservations){
  5. ($Client_Name,$O1,$O2,$O3,$O4,$O5,$O6,$O7,$O8,$O9,$Start_Date,$Start_Month,$Start_Year,$End_Date,$End_Month,$End_Year,$Check_Out_Hour,$Check_Out_Min)=split (/\|/,$line);    
  6. if($is == $sday) {$width ='30';}else{$width ='10';}
  7. if(($is < $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#9A9A9A';$Checkbox = '';}
  8. elsif(($is == $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#707070';$Checkbox = '';}
  9. elsif(($is > $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#FF0000';$Checkbox = '';}
  10. else{$bgcolor = '#00FF00';}
  11. if(($bgcolor eq '#00FF00')&&($is >= $sday))
  12. {$Checkbox = "<input type=checkbox name=\"RoomNum\" value=\"$is-$i-$rooms[$m-1]\">";}## (date-type-rumnum ###
  13. elsif($is < $sday){$Checkbox = '';}
  14. }# END OF foreach $line (@All_Reservations){
  15. }# END OF for($ca=0;$ca<=$Rooms_Reserved;$ca++)

The color meaning is:
Light gray, used room (any day prior to today)
Dark gray (room being used today)
Red, any reserved room not used yet (future reservation any date beyond today)
Green, any free room.

Hope you can straight me out... I want the script to display all reservations.
(Attached scripts in bundle
Thanx beforehand
Attached Files
File Type: zip (10.1 KB, 84 views)
Apr 13 '09 #1
Share this Question
Share on Google+
2 Replies

Expert 2.5K+
P: 4,059
Oh my goodness..... sorry mate, I'm not looking through that much code written without strict or even warnings. I hope to heaven that code is not being used on a real website, if it is, there is no way it will run for very long before crashing and burning. Sorry to be so negative sounding but you have a can of worms there.
Apr 14 '09 #2

P: 1
Move code in the script Reserved_Rooms.cgi from line #341
Expand|Select|Wrap|Line Numbers
  1. print"<td bgcolor=$bgcolor width=$width><center><b>$is<br>$Checkbox$HR</center></td>";
to line #339, i.e. before line
Expand|Select|Wrap|Line Numbers
  1.  }# END OF for($ca=0;$ca<=$Rooms_Reserved;$ca++) 
Dmitry Grasevitch
Apr 16 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.