Thanks that 'works' in that it no longer gives a parsing error, i now get "Fatal error: Cannot use [] for reading in /var/www/html/report2.php on line 36"
(same as if i use $$answer[])
The basic idea is that i've had a questionnaire online which people have filled in. Now i've come to do the results for it. The questions were all multiple choice so i've just stored the letter of the option they chose, (ie a, b or c).
What i am trying to do is to get arrays of answers for each question and then use 'array_count_values()' on each of these arrays to count the answers.
any suggestions
cheers
this is my code
$sql_reply_count = "select count(ID) from relocation";
$result_count = mysql_query ($sql_reply_count) or die (mysql_error());
$count = mysql_fetch_row($result_count) ;
$number_of_replies = $count[0] ;
?>
<p>Total Number of Replies = <? echo $number_of_replies ;
?>
</p>
<?
$sql_average_distance = "select avg(Q1) from relocation";
$result_average_distance = mysql_query ($sql_average_distance) or die(mysql_error());
$row_average_distance = mysql_fetch_row($result_average_distance);
$average_distance = $row_average_distance[0] ;
?>
<p>Average Distance travelled to work = <? echo $average_distance ;
?> miles
</p>
<?
$sql_answers = "select from relocation" ;
$result_answers = mysql_query ($sql_answers) or die( mysql_error());
while ($row_answers = mysql_fetch_array ($result_answers))
{
$questions = array('Q2', 'Q3', 'Q4', 'Q5', 'Q7', 'Q8', 'Q9', 'Q10_a', 'Q10_b', 'Q10_c', 'Q10_d', 'Q10_e', '10_f', 'Q12', 'Q13');
for ($number = 0; $number < count($questions); $number ++)
// loops through the questions in the array
{
//this loop forms an array of answers for each question in the $questions array
$question = $questions[$number];/this gives the question number ie Q2/
$answers = $question;
$answers .= "_answers";/this creates $answers = Q2_answers ready for variable variable*/
${$answers}[] = $row_answers[$question] ;
}
}
for ($i = 0; $i < count($questions); $i ++)
{
$answers = $question ;
$answers .= "answers";
$counts = $questions[$i];
$counts = "responses";
$$counts = array_count_values($$answers);
print "Question $questions[$i]";
/*while ($element = each ($$counts))
{
$answer = ["key"];
$number = ["value"];
$percentage = ($number/$number_of_replies)*100;
print "$answer : $percentage";
}*/
}
?>