Wrox Programmer Forums
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 13th, 2004, 06:53 PM
Registered User
Join Date: Jan 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default quiz total score

Hello - I'm new here, and very much a beginner, so please excuse me. ;)

I am making a quiz on one of my websites, and I want it to be scored with PHP.
All I want is to say what the correct answer is, and if they got that correct answer, add 1 to the score.
So, something like
if $name = "john" [ however you would say add 1 ] ..
else [ add nothing ].

Then I want the total printed out on the page.

An added bonus would be to have it write the top score to a text file, printing that on the page and overwriting it if someone beats that top score.

Any help would be MUCH appreciated - this thing is getting nowhere with just my miniscule knowledge.

Old January 13th, 2004, 07:10 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

Let's suppose you keep all the correct answers in an array. The array is indexed by the name of the input field.

Also, we'll suppose you keep the current high score in "highscore.dat".

$high_score_file = "highscore.dat";

$answers = array('name' => 'john', ...);

$score = 0;

$high_score = (int) file_get_contents($high_score_file);

echo "The current high score is {$high_score}.\n";

// Check each answer in the $answers array with the
// value submitted by the user.
foreach($answers as $input_field => $answer)
    if (isset($_POST[$input_field]) &&
        ($_POST['input_field'] == $answer))

// Print total score to user.
echo "Your score is {$score}.\n";

if ($score > $high_score)
    echo "You set a new high score!\n";

    // write new high score to file:
    $fp = fopen($high_score_file, 'w');
    if ($fp)
        fwrite($fp, "{$score}");


Take care,

Old January 14th, 2004, 12:22 AM
Registered User
Join Date: Jan 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

thank you so much! The script works, and I managed to put in a few extras for fun. :)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Algorithm to Score Global Quality-Quantity of File asgarcymed VBScript 0 November 24th, 2007 11:12 PM
Display highest test score kgs51 VB.NET 2002/2003 Basics 15 November 19th, 2004 11:02 AM
Coursebuilder Score Script fs22 Javascript 0 April 8th, 2004 09:37 AM
get max test score and corresponding date ssambr1 MySQL 1 January 24th, 2004 08:42 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.