Jump to content

Undefined offset


Andrew R

Recommended Posts

The following script checks to see if the user answer matches the correct answer. 

 

Form Fields are set up the following way...

 

user_answer[$i] | value = a

user_answer[$i] | value = b

user_answer[$i] | value = c

 

all radio fields (above)

 

orgid[$i] | value = $i

answer[$i] | value = (a or b, c)

 

all hidden fields (above)

 

$i = question id/number

 

Basically when question answer isn't selected an undefined offset error message is generated.  Adding if empty or isset messes up the order. 

 

 

foreach ($_POST['orgid'] as $k =>$v) {
$orgid[$k] = $v;
}

$k = 0;
$m = 0;
while ($k <= 19) {
if ($_POST['user_answer'][$k] == $_POST['answer'][$k])  {
$correct++;

unset ($_POST['user_answer'][$k]);
unset ($orgid[$k]);
} else {
$incorrect++;
}
$k++;
}

 

Any ideas/suggestions?

 

Thanks a million

Link to comment
https://forums.phpfreaks.com/topic/185009-undefined-offset/
Share on other sites

I dont see why this wouldnt work..

 

<?php
foreach ($_POST['orgid'] as $k =>$v) {
$orgid[$k] = $v;
}

$k = 0;
$m = 0;
while ($k <= 19) {
if (isset($_POST['user_answer'][$k]) && $_POST['user_answer'][$k] == $_POST['answer'][$k])  {
$correct++;

unset ($_POST['user_answer'][$k]);
unset ($orgid[$k]);
} else {
$incorrect++;
}
$k++;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/185009-undefined-offset/#findComment-976667
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.