mausie Posted September 21, 2006 Share Posted September 21, 2006 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): [code]$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; [/code]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:[code] 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; }[/code]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 greatMaurice Link to comment https://forums.phpfreaks.com/topic/21527-reading-arrays-in-function/ Share on other sites More sharing options...
trq Posted September 21, 2006 Share Posted September 21, 2006 You need to pass the $query array into the function. Eg;[code=php:0]function monthtotal($mon,$timeclass,$query)[/code] Link to comment https://forums.phpfreaks.com/topic/21527-reading-arrays-in-function/#findComment-96070 Share on other sites More sharing options...
mausie Posted September 21, 2006 Author Share Posted September 21, 2006 Darn that's some logic thinking I've missed.Many thanks for your help and extremely fast reply!grtz Link to comment https://forums.phpfreaks.com/topic/21527-reading-arrays-in-function/#findComment-96075 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.