Jump to content

Looking to create an online Quiz


doctor-Eggman

Recommended Posts

Sounds like you need to create a form in HTML and send the answers to some other page via an array.  Then just loop through the array and compare to the array which stores the correct answers and increase a counter every time the values in the arrays match.

 

Once you're done just divide the number correct by the size of the array and you'll have your percentage.  If there is some specific portion of code that you need help with then let us know.

Link to comment
Share on other sites

} else {

$num = (int) $_POST['num'];

$postedanswers = str_replace("_"," ",$_POST['answers']);

if ($postedanswers == $answers[$num]['0']) {

$_SESSION['score']++;

$_SESSION['correct'][] = $postedanswers;

} else {

$_SESSION['wrong'][] = $postedanswers;

}

if ($num < count($questions)-1) {

$num++;

} else {

$last = true;

$_SESSION['finished'] = 'yes';

}

 

I took that dudes advice and looked on google and got some good code that does what I want. Problem is, it only makes a score out of 10. I want to change it to a percentage. I really am awful at PHP and could use a hand. I think that is the stuff making the answer, how could I make it display a percentage?

Link to comment
Share on other sites

Ok I think I know what you were saying before. Instead of the marks going up by 1. I need to say find out what one percent of 20 is and make it go up by that right? Right now when the score goes up, it goes up by one at a time. I need it to go up by .20 or something. Problem is I cant seem to find whats making it go up by 1. Here is all the code from the test page. If ANYONE can help I will be insanely grateful.

 

	$_SESSION['user'] = $username;
	$_SESSION['score'] = 0;
	$_SESSION['correct'] = array(); 
	$_SESSION['wrong'] = array();
	$_SESSION['finished'] = 'no';
	if (isset($_SESSION['error']))
	unset($_SESSION['error']); 
	$num = 0;
} else {
	$random = rand(1,1000);
	$_SESSION['user'] = 'Anon'. $random;
	$_SESSION['score'] = 0;
	$_SESSION['correct'] = array(); 
	$_SESSION['wrong'] = array(); 
	$_SESSION['finished'] = 'no';
	$num = 0;
}
} else {
$num = (int) $_POST['num'];
$postedanswers = str_replace("_"," ",$_POST['answers']);
if ($postedanswers == $answers[$num]['0']) {
	$_SESSION['score']++;
	$_SESSION['correct'][] = $postedanswers; 
} else {
	$_SESSION['wrong'][] = $postedanswers;
} 
if ($num < count($questions)-1) {
	$num++;
} else {
	$last = true;
	$_SESSION['finished'] = 'yes';
}/php]

[code=php:0]<?php } else { 
$file = "leaders.xml";
$xml = simplexml_load_file($file);
$user = $xml->addChild('user');
$uname = $user->addChild('name',$_SESSION['user']);
$uscore = $user->addChild('score',$_SESSION['score']);
$xml->asXML("leaders.xml");

echo "<h2 id=\"score\">{$_SESSION['user']}, your final score is:</h2>\n
<h3>{$_SESSION['score']}/20</h3><h4>Verdict:</h4>";
if($_SESSION['score'] <= 5) echo "<p id=\"verdict\"><span>S</span>everely <span>H</span>indered <span>I</span>n the <span>T</span>est!</p>\n";
if(($_SESSION['score'] > 5) && ($_SESSION['score'] <= 10)) echo "<p id=\"verdict\"><span>C</span>ould <span>R</span>ead <span>A</span>nd <span>P</span>ractice more.</p>\n";
if(($_SESSION['score'] > 10) && ($_SESSION['score'] <= 15)) echo "<p id=\"verdict\"><span>A</span>cronyms a<span>R</span>e <span>S</span>o <span>E</span>asy!</p>\n";
if($_SESSION['score'] > 15) echo "<p id=\"verdict\"><span>S</span>uper <span>A</span>cronym <span>S</span>pecialist</p>";
echo "<p id=\"compare\"><a href=\"results.php\">See how you compare! <img src=\"images/arrow.png\" /></a></p>";
}
?>

Link to comment
Share on other sites

the simple answer would be to take the number they got right and divide it by the total number of questions.

 

$rightAnswer = $session['correct']; // Or whatever the code is for this.
$totalQuestions = $numberOfQuestions; // Same here.
$perc = $rightAnswer/$totalQuestions;

echo "You got ".$perc."% of the questions correct.";

Link to comment
Share on other sites

Ok I did get some code that shows a percentage. It shows it after the score out of 20 though. So it goes like " you scored 4/20 your percentage is 40" I am definatly moving in the right direction.

What I am looking for now is for the mark out of 20 to not be shown at all and only the percentage is shown. Also the score out of 20 goes to a leaderboard what I want is the percentage to go to the leaderboard instead.

I know I am a pain in the ass but Any help and I would be exstatic.

 

I will provide the code here.

 

This is the code in the test part that I belive will send it to the leaderboard and the one that shows the results.

<?php } else {
$file = "leaders.xml";
$xml = simplexml_load_file($file);
$user = $xml->addChild('user');
$uname = $user->addChild('name',$_SESSION['user']);
$uscore = $user->addChild('score',$_SESSION['score']);
$xml->asXML("leaders.xml");

echo "<h2 id=\"score\">{$_SESSION['user']}, your final score is:</h2>\n
<h3>{$_SESSION['score']}/20</h3><h4>Verdict:</h4>";

//changed code
$percent=$_SESSION['score']/20*100; //work out a percentage, divide the score by total an *100
echo "<p>Your Percentage: ".$percent."</p>";


if($_SESSION['score'] <= 5) echo "<p id=\"verdict\"><span>S</span>everely <span>H</span>indered <span>I</span>n the <span>T</span>est!</p>\n";
if(($_SESSION['score'] > 5) && ($_SESSION['score'] <= 10)) echo "<p id=\"verdict\"><span>C</span>ould <span>R</span>ead <span>A</span>nd <span>P</span>ractice more.</p>\n";
if(($_SESSION['score'] > 10) && ($_SESSION['score'] <= 15)) echo "<p id=\"verdict\"><span>A</span>cronyms a<span>R</span>e <span>S</span>o <span>E</span>asy!</p>\n";
if($_SESSION['score'] > 15) echo "<p id=\"verdict\"><span>S</span>uper <span>A</span>cronym <span>S</span>pecialist</p>";
echo "<p id=\"compare\"><a href=\"results.php\">See how you compare! <img src=\"images/arrow.png\" /></a></p>";
}
?>

 

This is the code in the XML file that is storing the the leader information

 

<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user>
    <name>Bobby</name>
    <score>10</score>
  </user>
  <user>
    <name>Billy</name>
    <score>1</score>
  </user>
</users>

 

and this is the show leaders code from the functions.

function showLeaders($file,$limit,$group = null) {
    $leaders = array();
   
    // Load the xml file and place all users and associated
    // scores into the 'leaders' array.
    $xml = simplexml_load_file($file);
    foreach($xml->user as $user) {
        $name = (string)$user->name;
        $score = (string)$user->score;
        $leaders[$name] = $score;
    }
       
    // Sort the leaders array numerically, highest scorers first.   
    arsort($leaders,SORT_NUMERIC);
    
    // Initialise our $counter variable to '1'.
    $counter = 1;

// Start a html ordered list to hold the leaders.
    $output = "<ul class=\"leaders\">\n";
    
    // Loop through the 'leaders' array and wrap each username and score
    // in <li> tags. If the user is the current $_SESSION['user'], wrap
    // the name/score in <strong> tags too.
    foreach ($leaders as $key => $value) {
        // Check that $counter is less than $limit.
        if ($counter <= $limit) {
            if ($key == $_SESSION['user']) {
                $output .= "<li><strong>$key:</strong> $value/20</li>\n";
            } else {
                $output .= "<li>$key: $value/20</li>\n";
            }
            // Check to see if $group parameter has been passed.
            // If it has, create separate lists according to the $group variable.
            if ($group) {
                // Use the modulus operator(%) to create new sub-list.
                if($counter % $group == 0) {
                    $output .= "</ul>\n<ul class=\"leaders\">\n";
                }
            }
        }
    // Increment the $counter. 
    $counter++;
    }
    // End the ordered list.
    $output .= "</ul>\n";
    
    // Print out the ordered list.
    echo $output;
}
    

 

You have all been really helpful so far and this could be me if I get this going. Any and all help would be INSANELY grateful it really would. Thank you!

 

 

Link to comment
Share on other sites

Ok I did get some code that shows a percentage. It shows it after the score out of 20 though. So it goes like " you scored 4/20 your percentage is 40"

 

First . . . your math function is a little off. 4 out of 20 is only 20% not 40%. You may try redoing your math function to something like this:

$scorePer = $right / $total;
$percent = $scorePer * 100;

 

For some reason your math function is working funny. I don't see how it got 40 percent.

 

As for doing it percent wise, try something like this:

if($_SESSION['percent'] <= 25) echo "<p id=\"verdict\"><span>S</span>everely <span>H</span>indered <span>I</span>n the <span>T</span>est!</p>\n";
if(($_SESSION['percent'] > 5) && ($_SESSION['percent'] <= 50)) echo "<p id=\"verdict\"><span>C</span>ould <span>R</span>ead <span>A</span>nd <span>P</span>ractice more.</p>\n";
if(($_SESSION['percent'] > 50) && ($_SESSION['percent'] <= 75)) echo "<p id=\"verdict\"><span>A</span>cronyms a<span>R</span>e <span>S</span>o <span>E</span>asy!</p>\n";
if($_SESSION['percent'] > 75) echo "<p id=\"verdict\"><span>S</span>uper <span>A</span>cronym <span>S</span>pecialist</p>";
echo "<p id=\"compare\"><a href=\"results.php\">See how you compare! <img src=\"images/arrow.png\" /></a></p>";
}

 

Basically, change $score to $percent and see if that works.

 

Hope that helps.

Link to comment
Share on other sites

I think if you change these areas to your percent variable:


$uscore = $user->addChild('score',$_SESSION['score']); // <- Here


echo "<h2 id=\"score\">{$_SESSION['user']}, your final score is:</h2>\n
<h3>{$_SESSION['score']}/20</h3><h4>Verdict:</h4>"; // <- Here

 

And the same in your xml:

 

<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user>
    <name>Bobby</name>
    <score>10</score> // <- Here
  </user>
  <user>
    <name>Billy</name>
    <score>1</score> // <- Here
  </user>
</users>

 

And in the leaders board:

 

function showLeaders($file,$limit,$group = null) {
    $leaders = array();
   
    // Load the xml file and place all users and associated
    // scores into the 'leaders' array.
    $xml = simplexml_load_file($file);
    foreach($xml->user as $user) {
        $name = (string)$user->name;
        $score = (string)$user->score; // <- Here
        $leaders[$name] = $score; // <- Here
    }
       
    // Sort the leaders array numerically, highest scorers first.   
    arsort($leaders,SORT_NUMERIC);
    
    // Initialise our $counter variable to '1'.
    $counter = 1;

// Start a html ordered list to hold the leaders.
    $output = "<ul class=\"leaders\">\n";
    
    // Loop through the 'leaders' array and wrap each username and score
    // in <li> tags. If the user is the current $_SESSION['user'], wrap
    // the name/score in <strong> tags too.
    foreach ($leaders as $key => $value) {
        // Check that $counter is less than $limit.
        if ($counter <= $limit) {
            if ($key == $_SESSION['user']) {
                $output .= "<li><strong>$key:</strong> $value/20</li>\n"; // <- Here
            } else {
                $output .= "<li>$key: $value/20</li>\n"; // <- Here
            }

 

Try changing those to your percentage value. i think that will take care of your problem. Oh, and take out the /20 that is there.

Link to comment
Share on other sites

I changed all you said to the percent variable and it worked...sort of. It is now having a really really odd problem. When I do the test and complete it. I get my score as normal. For example I will so the test and it will say "your percentage was : 95%" thats great....then when it shows the leaderboard the score will show i got say 70% or something. One time i tried it and got 100% and then the leaderboard showed that i got 60%.

 

So it is showing a percent...just the wrong one. Why on earth would it be doing this? Any idea why its giving me a seemingly random percent instead of the one i got.

Link to comment
Share on other sites

function showLeaders($file,$limit,$group = null) {
    $leaders = array();
   

    $xml = simplexml_load_file($file);
    foreach($xml->user as $user) {
        $name = (string)$user->name;
        $score = (string)$user->score;
        $leaders[$name] = $score;
    }
       

    arsort($leaders,SORT_NUMERIC);
    
    
    $counter = 1;
    
    
    $output = "<ul class=\"leaders\">\n";
    

    foreach ($leaders as $key => $value) {
       
        if ($counter <= $limit) {
            if ($key == $_SESSION['user']) {
                $output .= "<li><strong>$key:</strong> $value/20</li>\n";
            } else {
                $output .= "<li>$key: $value/20</li>\n";
            }
       
            if ($group) {
                // Use the modulus operator(%) to create new sub-list.
                if($counter % $group == 0) {
                    $output .= "</ul>\n<ul class=\"leaders\">\n";
                }
            }
        }
    
    $counter++;
    }
    
    $output .= "</ul>\n";
    
    
    echo $output;
}

 

That is the code I changed.

Link to comment
Share on other sites

Oooops that wasnt the code I changed at all. Thats the way it was sorry

function showLeaders($file,$limit,$group = null) { 
$leaders = array();


	$xml = simplexml_load_file($file);
	foreach($xml->user as $user) {
		$name = (string)$user->name;
		$percent = (string)$user->score;
		$leaders[$name] = $percent;
	}


	arsort($leaders,SORT_NUMERIC);


	$counter = 1;


	$output = "<ul class=\"leaders\">\n";


	foreach ($leaders as $key => $percent) {
		// Check that $counter is less than $limit.
		if ($counter <= $limit) {
			if ($key == $_SESSION['user']) {
				$output .= "<li><strong>$key:</strong> $percent%</li>\n";
			} else {
				$output .= "<li>$key: $percent%</li>\n";
			}

			if ($group) {
				// Use the modulus operator(%) to create new sub-list.
				if($counter % $group == 0) {
					$output .= "</ul>\n<ul class=\"leaders\">\n";
				}
			}
		}

	$counter++;
	}

	$output .= "</ul>\n";


echo $output;
}

 

Sorry about that. But yeah its still doing it.

Link to comment
Share on other sites

Any idea mage?

 

I think you finally lost me. Just looking at the code you provided doesn't tell me anything about what you changed. Maybe if you put comments in on what you changed I could see the problem easier. Maybe that or my weekend has taken a bigger toll on me then I thought.

 

Put comments in where you made the changes and try and show the original underneath and we will see where the problem is.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.