# Reading arrays in function

2 replies to this topic

### #1 mausie

mausie
• Members
• Member
• 16 posts

Posted 21 September 2006 - 01:23 PM

Hi all,

I'm reading out a database to get the total worked time of people on a month. I got this code I use on every single table(alot):

```\$arraycount = -1;
\$timetotal = 0;

foreach ( \$query['DATUM'] as \$date)
{

\$arraycount = \$arraycount + 1;

\$sub = substr(\$date,3,2);

if (\$sub == '01' && \$query['TIMECLASSID'][\$arraycount] == 3)
{
\$timetotal = \$timetotal +  \$query['CALCTIME'][\$arraycount];
}

}

print \$timetotal;```
This works fine,even tho I thought it would be much easier to make an function for this because it's alot of code doing this for like 30 tables. so I made:
``` function monthtotal(\$mon,\$timeclass)
{
\$arraycount = -1;
\$timetotal = 0;

foreach ( \$query['DATUM'] as \$date)
{

\$arraycount = \$arraycount + 1;

\$sub = substr(\$date,3,2);

if (\$sub == \$mon && \$query['TIMECLASSID'][\$arraycount] == \$timeclass)
{
\$timetotal = \$timetotal +  \$query['CALCTIME'][\$arraycount];
}

}
return \$timetotal;
}```

But i get Invalid argument in foreach() everytime I use this function. Because, of what I think it can't read the \$query. But why is this?
Could any1 help me? That would be great

Maurice

### #2 trq

trq
• Staff Alumni
• 31,041 posts

Posted 21 September 2006 - 01:30 PM

You need to pass the \$query array into the function. Eg;

```function monthtotal(\$mon,\$timeclass,\$query)
```

### #3 mausie

mausie
• Members
• Member
• 16 posts

Posted 21 September 2006 - 01:39 PM

Darn that's some logic thinking I've missed.
Many thanks for your help and extremely fast reply!

grtz

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users