Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Problem with the loop!

    I have this problem for the looping which repeating the schedule time multiple times as because of the loop. Here is the script..and is high lighted part is the bug i problem encounter. Thank You

    PHP Code:
    <html>
    <body>
    <form>
    <?    
        
        
    function ConvertDate($sql_date
        {
        
    $date=strtotime($sql_date);
        
    $final_date=date("Y-m-d"$date);
        return 
    $final_date;}
         
        function 
    ConvertTime($sql_date
        {
        
    $date=strtotime($sql_date);
        
    $final_date=date("g:i a"$date);
        return 
    $final_date;
        }
    $timeStrt='9:00';
    //3 bookers testing data!
    for($i=0;$i<3;$i++)
    {
        
    $name[$i]="mr $i";
    }
        
    $time[0]="10:00 am";
        
    $time[1]="11:15 am";
        
    $time[2]="10:00 am";
        
    $room[0]=1;
        
    $room[1]=0;
        
    $room[2]=1;
        
    //testing data ends here
    echo "<table border=1><tr>";
    //simulation 2 rooms
    for($j=0;$j<2;$j++)
    {
    echo 
    "<td><table border=1>";
    echo 
    "<tr><td> ROOM: $j</td></tr>";
    echo
    "
            <tr><td>Time</td><td>Book</td></tr>
            "
    ;
            for(
    $k=0;$k<=28800;$k=$k+900)//8hours and 15min apart
            
    {    
                [
    COLOR="Red"]//these 3 persons is the main problem for resulting the time repeating! BUG[/COLOR]
                
    for($i=0;$i<3;$i++)
                if(
    ConvertTime(date('H:i'strtotime($timeStrt) +$k)) == $time[$i] && $room[$i]==$j)
                
    //if time of the display time = to book time and booked room = to room then show
                
    {
                    echo 
    "<tr><td>".ConvertTime(date('H:i'strtotime($timeStrt) +$k))."+".$name[$i]."</td>
                            <td>Yes</td></tr>"
    ;
                }
                else
    //show time and the place is empty for radio button to select
                
    {
                    echo 
    "<tr><td>".ConvertTime(date('H:i'strtotime($timeStrt) +$k))."</td><td><input type='radio' name='RadioGroup' value='$j ".ConvertTime(date('H:i'strtotime($timeStrt) +$k))."'></td></tr>";
                }
            }
            echo 
    "</td></table>";
    }
    echo
    "</td></tr></table>";

    ?>
    </form>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Apr 2011
    Posts
    286
    Thanks
    2
    Thanked 39 Times in 39 Posts
    You should be checking if the room is booked in the for($k) loop, but not outputting anything till outside of the loop, where you use the results to say whether the room is available or not, otherwise you are repeating the same time 3 times, for each person.

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    The problem is that
    PHP Code:
     for($i=0;$i<3;$i++) 
                if(
    ConvertTime(date('H:i'strtotime($timeStrt) +$k)) == $time[$i] && $room[$i]==$j
    resulting the time looping multiple times, how do i solve it?

  • #4
    Regular Coder
    Join Date
    Apr 2011
    Posts
    286
    Thanks
    2
    Thanked 39 Times in 39 Posts
    Instead of outputting the result, create a variable just before your loop like:
    Code:
    $isBooked = false;
    In the loop, if the persons time matches the current time, you set $isBooked = true, but do nothing if it does not match.

    Then outside the loop is where you output the time and whether it is booked ($isBooked)

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    pardon my ignorance but when i follow the $isBooked, resulted only passing back last value

    PHP Code:
    for($j=0;$j<2;$j++)
    {
    echo 
    "<td><table border=1>";
    echo 
    "<tr><td> ROOM: $j</td></tr>";
    echo
    "
            <tr><td>Time</td><td>Book</td></tr>
            "
    ;
            for(
    $k=0;$k<=28800;$k=$k+900)//8hours and 15min apart
            
    {    
                
    //these 3 persons is the main problem for resulting the time repeating! BUG
                
    for($i=0;$i<3;$i++)
                if(
    ConvertTime(date('H:i'strtotime($timeStrt) +$k)) == $time[$i] && $room[$i]==$j)
                
    //if time of the display time = to book time and booked room = to room then show
                
    {
                    
    $isBooked[$k][$j] = true;
                }
                else
    //show time and the place is empty
                
    {
                    
    $isBooked[$k][$j] = false;
                }
            }

            for(
    $k=0;$k<=28800;$k=$k+900)
            if(
    $isBooked[$k][$j]==true)
            {
                echo 
    "<tr><td>".ConvertTime(date('H:i'strtotime($timeStrt) +$k))."</td>
                <td>Yes</td></tr>"
    ;
            }
            else
            {
                echo 
    "<tr><td>".ConvertTime(date('H:i'strtotime($timeStrt) +$k))."</td><td><input type='radio' name='RadioGroup' value='$j ".ConvertTime(date('H:i'strtotime($timeStrt) +$k))."'></td></tr>";
            }
            echo 
    "</td></table>"
    is any possible you could show me how you write it? Million thanks!

  • #6
    Regular Coder
    Join Date
    Apr 2011
    Posts
    286
    Thanks
    2
    Thanked 39 Times in 39 Posts
    Best if I tell you how so it might force you to understand it a bit better.

    You need to display it within the for($k...) loop, not as a separate loop.

    Also, I told you not to set $isBooked in the else, you do not need an else to that statement, it will only set $isBooked to false when its true already from a previous person.

    Instead you need to do $isBooked = false; just above where you define the for($i...) loop, and within the for($k...) loop.

    Also, you might want to use { } for every for loop, so you can see it better, and instead of using $i,$j,$k, you should use meaningful names for the loops so you know what the values are for.

    $k = $roomTime
    $j = $roomNumber
    $i = $personIndex

  • #7
    New Coder
    Join Date
    Apr 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thank You! it works!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •