View Full Version : questionnaire error
nuclearvapid
10-12-2003, 10:24 AM
what is wrong with this?
<?php
$score = 0;
if ($q1 == a) $score == $score + 1;
echo ("Your quiz score is ".$score."");
?>
it wont count...
:(
Rydberg
10-12-2003, 10:41 AM
First of all, this goes in the PHP forum.
A lot of mistakes in this one...
$q1 is not defined.
'a' is not a defined constant.
You're using '==' where you should be using '='
<?php
$score = 0;
$q1 = 5;
define('a', 5);
if ($q1 == a)
$score = $score + 1; // or just $score++;
echo ("Your quiz score is ".$score."");
?>
Try to apply something similar to your script (I assume your script is bigger than this). Can't really help you more without seeing more of the script.
nuclearvapid
10-12-2003, 11:25 AM
ok - imagine you have a simple questionnaire...
in the question.html form
<tr valign="top">
<td height="40" width="3%">1.</td>
<td width="37%">one</td>
<td align="center" width="16%">a) <input type="radio" name="q1" value="a"></td>
<td align="center" width="16%">b) <input type="radio" name="q1" value="b"></td>
<td align="center" width="17%">c) <input type="radio" name="q1" value="c"></td>
<td width="7%"> </td>
</tr>
sent to answers.php
<php?
$score = 0;
if ($q1 == a) $score = $score++;
echo ("Your quiz score is ".$score."");
?>
which wont validate or count...
and this next bit which does work (so please no one tell me it doesn't cause it does...)
<tr valign="top">
<td height="40" width="3%">1.</td>
<td width="37%">one</td>
<td align="center" width="16%">a) <input type="radio"<?php if ($q1 == a){echo (' checked');} ?>></td>
<td align="center" width="16%">b) <input type="radio"<?php if ($q1 == b){echo (' checked');} ?>></td>
<td align="center" width="17%">c) <input type="radio"<?php if ($q1 == c){echo (' checked');} ?>></td>
<td align="center" width="7%">
<?php if ($q1 == a){echo ('correct');} else {echo ('<b>a</b>');} ?>
</td>
</tr>
you can see, the out put does recognise that the varible value comes from the form - but where does it go wrong in the counting?
nuclearvapid
10-12-2003, 11:29 AM
i moved this to the php list and i explained the problem a little better
scoutt
10-12-2003, 12:26 PM
Originally posted by nuclearvapid
what is wrong with this?
<?php
$score = 0;
if ($q1 == a) $score == $score + 1;
echo ("Your quiz score is ".$score."");
?>
it wont count...
:(
how about this
<?php
$score = 0;
if ($_POST['q1'] == 'a') $score++;
echo "Your quiz score is ". $score;
?>
scoutt
10-12-2003, 12:29 PM
Originally posted by nuclearvapid
and this next bit which does work (so please no one tell me it doesn't cause it does...)
how can it when it is the same as above which doesn't work.
$q1 == a
that will NEVER be true because a is a string and you didn't quote it.
$q1 == 'a'
so yeah I will tell you it doesn't work. clear your cache and you will see it doesn't work. does the radio button get checked? after you submit?
nuclearvapid
10-13-2003, 02:38 AM
funny enough yes!
go try it yourself
http://www.mfl-ucan.co.uk/l1/l1_h_smod_r3q.html
nuclearvapid
10-13-2003, 04:24 AM
found problem! while 'my' code does work - it only works with older versions of php. What you/scout posted is correct and while it doesn't work on legacy versions, it DOES works much better in up to date versions.
Thank you.
Onto next stage of dev!
Rydberg
10-13-2003, 04:57 AM
Glad you worked it out.
I tried the quiz, and having seen your code, and my french not being what it once was, I put 'a' on all questions, hoping to have at least one correct answer.. Dang! Tricky.
scoutt
10-13-2003, 10:03 AM
Originally posted by nuclearvapid
found problem! while 'my' code does work - it only works with older versions of php. What you/scout posted is correct and while it doesn't work on legacy versions, it DOES works much better in up to date versions.
Thank you.
Onto next stage of dev!
yup, you should never use old versions of php. they are out dated and insecure. but the old version of php should still treat strings the same. but I will take your word for it as I don't run anything less then 4.1.2
vBulletin® v3.6.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.