Welcome to WebmasterWorld Guest from 54.226.183.49

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

looping problem....

Appending to the array.

     

woldie

10:17 am on Jan 17, 2005 (gmt 0)

10+ Year Member



Hi,

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]

dkin

11:13 am on Jan 17, 2005 (gmt 0)

10+ Year Member



with that much code I doubt you will get any help.

If you can narrow down the problem to a section it would help, are you getting any errors? if so what line are they on?

woldie

3:20 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



Thanks Dkin,

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[]='';
}
?>

jatar_k

4:47 pm on Jan 17, 2005 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



what do you mean by appending to the array? Do you mean a value in a single element? Do you mean extra values?

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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month