foraech loop inside a for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • virtualweb
    New Member
    • Aug 2007
    • 30

    foraech loop inside a for loop

    Hello:

    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.

    Code:
    3
    John Smith|||||||||Day:,Eve:|25|4|2009|25|4|2009|11|30
    James Taylor|||||||||Day:,Eve:|26|4|2009|26|4|2009|11|30
    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:

    Code:
         for($ca=0;$ca<=$Rooms_Reserved;$ca++)
    {
    ($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]);	
    if($is == $sday) {$width ='30';}else{$width ='10';}
    if(($is < $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#9A9A9A';$Checkbox = '';}
    elsif(($is == $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#707070';$Checkbox = '';}
    elsif(($is > $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#FF0000';$Checkbox = '';}
    else{$bgcolor = '#00FF00';}
    if(($bgcolor eq '#00FF00')&&($is >= $sday))
    {$Checkbox = "<input type=checkbox name=\"RoomNum\" value=\"$is-$i-$rooms[$m-1]\">";}## (date-type-rumnum ###
    elsif($is < $sday){$Checkbox = '';}
    
    }# 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:

    Code:
       for($ca=0;$ca<=$Rooms_Reserved;$ca++)
    {
    push(@All_Reservations, $data[$ca]);
    foreach $line (@All_Reservations){
    ($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);	
    if($is == $sday) {$width ='30';}else{$width ='10';}
    if(($is < $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#9A9A9A';$Checkbox = '';}
    elsif(($is == $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#707070';$Checkbox = '';}
    elsif(($is > $sday)&&($is >= $Start_Date)&&($is <= $End_Date)&&($year == $Start_Year)){$bgcolor = '#FF0000';$Checkbox = '';}
    else{$bgcolor = '#00FF00';}
    if(($bgcolor eq '#00FF00')&&($is >= $sday))
    {$Checkbox = "<input type=checkbox name=\"RoomNum\" value=\"$is-$i-$rooms[$m-1]\">";}## (date-type-rumnum ###
    elsif($is < $sday){$Checkbox = '';}
    }# END OF foreach $line (@All_Reservations){
    }# 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 Reserve.zip)
    Thanx beforehand
    VirtualWeb
    Attached Files
    Last edited by pbmods; Apr 13 '09, 10:43 PM. Reason: Removed annoying bold tags.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

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

    Comment

    • Dmitry Grasevitch
      New Member
      • Apr 2009
      • 1

      #3
      Move code in the script Reserved_Rooms. cgi from line #341
      Code:
      print"<td bgcolor=$bgcolor width=$width><center><b>$is<br>$Checkbox$HR</center></td>";
      to line #339, i.e. before line
      Code:
       }# END OF for($ca=0;$ca<=$Rooms_Reserved;$ca++)
      WBR,
      Dmitry Grasevitch
      Last edited by eWish; Apr 16 '09, 11:47 PM. Reason: Fixed code tags

      Comment

      Working...