Forum Moderators: coopster
I'm having a spot of bother with my code. What I have is a for loop and what it does it displays the available times for each engineer on the day selected.
Now what the code does, is displays the available times for the first engineer and then in the next table cell its displays the available times for the next engineer, but what it does it adds onto the array the previous engineers available times. This is repeated with each engineer in each table cell.
Now I've been trying to rectify this problem and I can't figure it out.
Can anyone help me out?
Here's the code:
<modnote code in msg3 - jatar_k>
Many Thanks
Woldie. :o)
[edited by: jatar_k at 4:36 pm (utc) on Jan. 17, 2005]
Yeah you are right about the code, just thought it might help someone if I give the full code.
Anyhow, I think the problem lies with not setting the array to 0, because its appending somewhere.
I've trimmed down the code, maybe someone can spot where I am going wrong.
Many thanks in advance.
Code:
<?
for ($x=0;$x<$numrows;$x++)
{
# Query to get times taken by each engineer
$result=mysql_query("select bktimes from booking_times,streamline_shows,cmsengineers
where booking_times.shid=streamline_shows.shid
and showname='$DBshowname'
and bkdate='$showdate'
and booking_times.engineernum=cmsengineers.engineernum
and booking_times.engineernum=".$disp_quest[$cnt]."
order by bktimes");
$num=mysql_num_rows($result);
if ($num!=0)
{
print_r($times);
// Works out difference if more than 1 is found
#$num=mysql_num_rows($result);
while (list($DBbktimes)=mysql_fetch_row($result))
$times[]=$DBbktimes;
# list all the times from 10:00 to 17:00 inclusive
$start = mktime(10,0,0,date('m'),date('d'),date('Y'));
for ($i=$start;$i<=$start+25200;$i+=900)
{
$time = date("H:i",$i);
$timeav[]= $time;
}
# this works out the diiference between the two arrays
$res=array_diff($timeav, $times);
}
else
{
echo 'hello2';
// otherwise just list all the times available
$start = mktime(10,0,0,date('m'),date('d'),date('Y'));
for ($i=$start;$i<=$start+25200;$i+=900)
{
$time_all = date("H:i",$i);
$alltimes[]=$time_all;
}
}
?>
Below is the code where I think it could be appending to the end of the array. The $res variable could be culprit.
<?
if (isset($res))
{
# displays the available times for the engineer
foreach ($res as $var => $value)
{
?>
<tr>
<td class="labelcell"><? echo $value;?></td>
<td class="radiotype"><input type="radio" name="newtime" value="<? echo $value?>.<? echo $disp_quest[$cnt]?>"></td>
</tr>
<?
}
}
if (isset($alltimes))
{
foreach ($alltimes as $var => $value)
{
?>
<tr>
<td class="labelcell"><? echo $value;?></td>
<td class="radiotype"><input type="radio" name="newtime" value="<? echo $value?>.<? echo $disp_quest[$cnt]?>"></td>
</tr>
<?
}
}
$cnt++;
$n++;
unset($times);
unset($res);
#$alltimes[]='';
$res[]='';
}
?>
I don't really understand how or where it is misbehaving so I don't know what to look for.
what I usually do
1. find the spot where the code is noticed to misbehave
2. echo the valu of all related vars
3. move backwards in the script echoing the value of the var in question before and after each time you do anything with that var