Forum Moderators: coopster

Message Too Old, No Replies

compare two arrays (quiz)

Lost between arrray_intersect and var var

         

henry0

6:55 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$answer comes from:
<td align='left' width='15%'>True: <input type='checkbox' name='answer[]' value='y'><br />
False: <input type='checkbox' name='answer[]' value='n'></td></tr>";

$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);
}
}
}

PHP_Chimp

7:22 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I must not be understanding you correctly, as if you are just checking if $answer is the same as $row['answer'] you only need an if statement.

if ($answer == $row['answer'] {
// they are the same
}
else {
// they are different
}

So assuming that what I have above is of no use to you ;)
is

//$c=count($key, $answer);
$c = count($both);

of more use?

coopster

7:31 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looks like the answers are an array too. So, I think the question is how do I compare two arrays of questions and answers? The first array is an array of the questions and their corresponding correct answers from the database table ...
$correct = array( 
'Q1' => 0,
'Q2' => 0,
'Q3' => 1
);

... and the second array is the $_POST results form a <form>:
array( 
'Q1' => 1,
'Q2' => 0,
'Q3' => 1
);

So, in this case the first question was answered incorrectly. Can you confirm, henry0?

henry0

7:38 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks,
Not really, unless I do not get your concept

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.

henry0

7:39 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Coopster, yes you got it thanks

PHP_Chimp

7:51 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh got it now...I think...


$_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]

henry0

8:05 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that we are getting closer
however
it results in 0

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";
>>>>>