Forum Moderators: coopster
$key is the correct previously DB inserted answer to the questions
I tried many variations, from var/var to other forms of array function
until I checked here to see if someone had a similar question
I found a way using array_intersect
but I always get
" count() expects parameter 2 to be long, array given in line...
which is:$c=count($key, $answer);
Here is what I have
I try to compare both arrays
and figure if correct DB input == POSTED answers
is there a way to get it working?
$answer=$_POST['answer']; // POSTED answers
$counter2=0;
while ($row=$db->fetch_array($result) )
{
$key = $row['answer'];
$counter2++;
}
$both=array('$answer', '$key');
$c=count($key, $answer);
for($i=0, $u=count($both)-1; $i<$u; $i++)
{
for ($j=$i + 1; $j<$c; $j++)
{
$dupes=array_intersect(${$both[$i]}, ${$both[$j]} );if (count($dupes))
{
print_r($dupes);
}
}
}
if ($answer == $row['answer'] {
// they are the same
}
else {
// they are different
}
//$c=count($key, $answer);
$c = count($both);
$correct = array(
'Q1' => 0,
'Q2' => 0,
'Q3' => 1
);
array(
'Q1' => 1,
'Q2' => 0,
'Q3' => 1
);
In the DB are the correct answers,($key) per each row it has only "y" or "n"
$answer carries the POST answers in same mode only y or n.
I need to find how many times a Y from post equal a Y from the DB (then I'll do a % for the result.)
POST and DB rows could be any number so we need to loop through.
$_POST['answer'] = array ('Q1'=>1, 'Q2'=>0, 'Q3'=>1);
$correct = array ('Q1'=>0, 'Q2'=>0, 'Q3'=>1);
$right = 0; // number of correct answers
$num_questions = count($correct); // count questions in case someone hasnt answered a question
for ($i=1;$i<=$num_questions;$i++) {
if ($_POST['answer']["Q$i"]== $correct["Q$i"]) {
$right++;
}
}
$percentage = ($right/$num_questions)*100;
<edit>
changed it so it should loop properly.
V1.02 another change.
V1.03 removed a redundant loop...and changed it so that you get a mark for the correct answer, not the wrong one.
Im giving up for the evening. Someone else who is more awake can come and play.
[edited by: PHP_Chimp at 8:01 pm (utc) on Jan. 3, 2008]
I think it's due to the fact that we are not checking for "Y" in order to compare if row with Y from DB == row with Y from post
<<<<
$answer=$_POST['answer'];
$counter2=0;
while ($row=$db->fetch_array($result) )
{
$key = $row['answer'];
$counter2++;
}
//$_POST['answer'] = array ('Q1'=>1, 'Q2'=>0, 'Q3'=>1);
//$correct = array ('Q1'=>0, 'Q2'=>0, 'Q3'=>1);
$right = 0; // number of correct answers
$num_questions = count($key); // count questions in case someone hasnt answered a question
for ($i=0;$i<$num_questions;$i++) {
while ($x = $i) {
if ($_POST['answer'][$i]!= $key[$i]) {
$right++;
}
$x++;
}
}
$percentage = ($right/$num_questions)*100;
echo"$percentage";
>>>>>