/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Scoring a Quiz

I have a true/false quiz that I would like to show the pass/fail results.

I get zero every time even if I answer all the questions correctly.

[code=php]<?php
$a1 = “true”;
$a2 = “false”;
$a3 = “false”;
$a4 = “true”;
$a5 = “true”;
$a6 = “false”;
$a7 = “false”;
$a8 = “true”;
$a9 = “true”;
$a10 = “false”;
$a11 = “false”;
$a12 = “true”;
$a13 = “true”;
$a14 = “false”;

$name=$_POST[‘name’];
$q1=$_POST[‘q1’];
$q2=$_POST[‘q2’];
$q3=$_POST[‘q3’];
$q4=$_POST[‘q4’];
$q5=$_POST[‘q5’];
$q6=$_POST[‘q6’];
$q7=$_POST[‘q7’];
$q8=$_POST[‘q8’];
$q9=$_POST[‘q9’];
$q10=$_POST[‘q10’];
$q11=$_POST[‘q11’];
$q12=$_POST[‘q12’];
$q13=$_POST[‘q13’];
$q14=$_POST[‘q14’];

$score=0;

if ($q1== $a1) { $score ++; }
if ($q2== $a2) { $score ++; }
if ($q3== $a3) { $score ++; }
if ($q4== $a4) { $score ++; }
if ($q5== $a5) { $score ++; }
if ($q6== $a6) { $score ++; }
if ($q7== $a7) { $score ++; }
if ($q8== $a8) { $score ++; }
if ($q9== $a9) { $score ++; }
if ($q10== $a10) { $score ++; }
if ($q11== $a11) { $score ++; }
if ($q12== $a12) { $score ++; }
if ($q13== $a13) { $score ++; }
if ($q14== $a14) { $score ++; }

if ($score>=10) {
$testresult = ‘Your score is ‘.$score.’ <br />Please click <a href=”certificate.php”>here</a> to print your certificate.’;
} else {
$testresult = ‘Your score is ‘.$score.’ <br /><br />Unfortunately you did not pass the course.<br />Please review your answers and try again.</p>’;
}
?>
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@cbVisionNov 29.2011 — The problem may be that you're comparing an actual "true/false" type to a string that's equal to "true" and "false".

Try removing the quotes:

[CODE]
$a1 = TRUE;
$a2 = FALSE;
$a3 = FALSE;
...
[/CODE]


I'm not sure if case matters or not.

The opposite way would be converting your post data to strings and then comparing.
Copy linkTweet thisAlerts:
@beylahauthorNov 29.2011 — it was the capital letters
×

Success!

Help @beylah spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...