joejoebiggs Posted December 15, 2009 Share Posted December 15, 2009 Hello, I am in real need of some help. My table (responses) is laid out like: id - repsonse id emp_num - employee number ans1 - rating 1-10 dtaken - date submitted wtaken - week of the year submitted I am trying to poll the database to return surveys answered during a given date, then I need it to give me the average rating for each week during that given period. Any insight into this would be very helpful as I am pulling my hair out! Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/ Share on other sites More sharing options...
killerb Posted December 15, 2009 Share Posted December 15, 2009 Dunno if this is the easiest or most elegant but it will achieve your requirements. You may need to tweak for performance on large data sets. This is untested, written quick for demonstration of how I have solved this sort of problem before: <?php $query = "select * from .. where dtaken >= :start_date and dtaken <= :end_date order by wtaken ASC, dtaken ASC" $bind = array('start_date'=>$start_date, 'end_date'=>$end_date); $rows = $DB->fetchAll($query, $bind); $week_stats = array(); foreach($rows as $row){ if(!array_key_exists($row['wtaken'], $week_stats)){ $week_stats[$row['wtaken'] = array( 'total' => 0, 'num' => 0 ); } // store the total of all averages for calculating averages $week_stats[$row['wtaken']]['total'] += $row['ans1']; // count number of answers for calculating averages $week_stats[$row['wtaken']]['num']++; } // calculate the averages foreach($week_stats as $stat){ $week_stats['average'] = $stat['total']/$stat['num']; } var_dump($week_stats); Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-978021 Share on other sites More sharing options...
joejoebiggs Posted December 15, 2009 Author Share Posted December 15, 2009 Thanks for the help, however, when I try running this I get am HTTP 500 error. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-978045 Share on other sites More sharing options...
fenway Posted December 15, 2009 Share Posted December 15, 2009 Turn on php errors. Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-978046 Share on other sites More sharing options...
roopurt18 Posted December 15, 2009 Share Posted December 15, 2009 Your wtaken column is redundant as you can always calculate the week of the year based off your dtaken column. This might work: <?php $start_tm = date( 'Y-m-d H:i:s', strtotime( '6/1/2009' ) ); $end_tm = date( 'Y-m-d H:i:s', strtotime( '1/1/2010' ) ); $select_stmt = " SELECT r.`id`, r.`emp_num`, r.`ans1`, r.`dtaken`, r.`wtaken` FROM `responses` AS `r` INNER JOIN ( SELECT DATE_FORMAT( `dtaken`, '%U' ) AS `week_of_year`, COUNT(*) AS `number_of_responses`, AVG( ans1 ) AS `average_rating` FROM `responses` WHERE `dtaken` BETWEEN '{$start_tm}' AND '{$end_tm}' GROUP BY DATE_FORMAT( `dtaken`, '%U' ) ) AS `s` ON DATE_FORMAT( r.`dtaken`, '%U' )=s.`week_of_year` WHERE r.`dtaken` BETWEEN '{$start_tm}' AND '{$end_tm}' "; $q = mysql_query( $select_stmt ); if( ! $q ) throw new Exception( 'Select error: ' . mysql_error() ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-978060 Share on other sites More sharing options...
joejoebiggs Posted December 16, 2009 Author Share Posted December 16, 2009 killerb, I get the error Parse error: syntax error, unexpected T_VARIABLE in /var/www/html/results/date_rpt.php on line 8 Here is my full code. <? // Make a MySQL Connection mysql_connect("localhost", "******", "*******") or die(mysql_error()); mysql_select_db("survey") or die(mysql_error()); $start_date = $_GET['date_st']; $end_date = $_GET['date_e']; $query = "select * from .. where dtaken >= :start_date and dtaken <= :end_date order by wtaken ASC, dtaken ASC" $bind = array('start_date'=>$start_date, 'end_date'=>$end_date); $rows = $DB->fetchAll($query, $bind); $week_stats = array(); foreach($rows as $row){ if(!array_key_exists($row['wtaken'], $week_stats)){ $week_stats[$row['wtaken'] = array( 'total' => 0, 'num' => 0 ); } // store the total of all averages for calculating averages $week_stats[$row['wtaken']]['total'] += $row['ans1']; // count number of answers for calculating averages $week_stats[$row['wtaken']]['num']++; } // calculate the averages foreach($week_stats as $stat){ $week_stats['average'] = $stat['total']/$stat['num']; } var_dump($week_stats); ?> Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-978499 Share on other sites More sharing options...
killerb Posted December 16, 2009 Share Posted December 16, 2009 Parse errors mean the code is invalid PHP format, something is missing and it invalidates the PHP code so it cannot be interpreted: Needs a semicolon after , dtaken ASC" Needs a right square bracket after $week_stats[$row['wtaken'] Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-978708 Share on other sites More sharing options...
joejoebiggs Posted December 16, 2009 Author Share Posted December 16, 2009 Thanks for the update. I am now getting an unexpected T_IF on line 14. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-978719 Share on other sites More sharing options...
killerb Posted December 16, 2009 Share Posted December 16, 2009 No can't help without getting into debugging (process of identifying cause of problem), therefore chargeable work. Sorry, not taking on small jobs presently. Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-978772 Share on other sites More sharing options...
Kryptix Posted December 16, 2009 Share Posted December 16, 2009 Thanks for the update. I am now getting an unexpected T_IF on line 14. Any ideas? This is nothing to do with MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-978790 Share on other sites More sharing options...
joejoebiggs Posted December 17, 2009 Author Share Posted December 17, 2009 Thanks for the update. I am now getting an unexpected T_IF on line 14. Any ideas? This is nothing to do with MySQL. 1. Thanks for the helpful insight 2. This was moved here by a mod, so if you don't like it here, go talk to them. Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-979117 Share on other sites More sharing options...
roopurt18 Posted December 17, 2009 Share Posted December 17, 2009 Did you figure out what was causing the parse error? Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-979301 Share on other sites More sharing options...
joejoebiggs Posted December 18, 2009 Author Share Posted December 18, 2009 Not yet... Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-979812 Share on other sites More sharing options...
fenway Posted December 18, 2009 Share Posted December 18, 2009 If you're still having trouble, this thread really should be in the PHP Coding Help forum... let's wait and see. Quote Link to comment https://forums.phpfreaks.com/topic/185271-php-mysql-help/#findComment-979825 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.