...

# Problem with the loop!

grayfoxmy
04-18-2011, 09:59 PM
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:D

<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
{
//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
{
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 "</td></table>";
}
echo"</td></tr></table>";

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

Wojjie
04-18-2011, 10:08 PM
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.

grayfoxmy
04-18-2011, 10:11 PM
The problem is that

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?

Wojjie
04-18-2011, 10:21 PM
Instead of outputting the result, create a variable just before your loop like:
\$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)

grayfoxmy
04-18-2011, 10:34 PM
pardon my ignorance but when i follow the \$isBooked, resulted only passing back last value

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 "</td></table>";
is any possible you could show me how you write it? Million thanks!

Wojjie
04-18-2011, 10:40 PM
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

grayfoxmy
04-18-2011, 11:06 PM
Thank You! it works!:cool: