doctor-Eggman Posted March 7, 2010 Share Posted March 7, 2010 You know something that I can set some questions and the PHP will tally up all the answers and give me a percentage based on how many were correct. Does anyone have something like that they can help me with? Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/ Share on other sites More sharing options...
gamblor01 Posted March 7, 2010 Share Posted March 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1022705 Share on other sites More sharing options...
jl5501 Posted March 7, 2010 Share Posted March 7, 2010 You also need to decide if you are going to show all the questions on the screen at once, or 1 at a time with a time limit Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1022819 Share on other sites More sharing options...
ohdang888 Posted March 8, 2010 Share Posted March 8, 2010 http://lmgtfy.com/?q=online+quiz+php+tutorial Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1022830 Share on other sites More sharing options...
Rustywolf Posted March 8, 2010 Share Posted March 8, 2010 if($question == "answer") { $num++; } do taht for every question. then to get a percentage $num = $num / 100; $num = $num * $question_amount; Now $num should be the percentage. $question_amount is the amount of questions Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1022894 Share on other sites More sharing options...
doctor-Eggman Posted March 10, 2010 Author Share Posted March 10, 2010 } 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? Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1024185 Share on other sites More sharing options...
Daniel0 Posted March 10, 2010 Share Posted March 10, 2010 Problem is, it only makes a score out of 10. I want to change it to a percentage. "Percent" means "per hundred", so what do you think you need to do? [math]\frac{1}{100} = 0.01 = 1\%[/math] Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1024199 Share on other sites More sharing options...
doctor-Eggman Posted March 10, 2010 Author Share Posted March 10, 2010 Jeez you are really calling out that I am a dope here haha. I really am stuck. Is that even the right code I put? I am not sure what to do Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1024222 Share on other sites More sharing options...
doctor-Eggman Posted March 11, 2010 Author Share Posted March 11, 2010 was that even the right code I put? I am really bad at this. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1024693 Share on other sites More sharing options...
salathe Posted March 11, 2010 Share Posted March 11, 2010 was that even the right code I put? No. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1024709 Share on other sites More sharing options...
doctor-Eggman Posted March 11, 2010 Author Share Posted March 11, 2010 Damn that was the only code I thought looked like it was scoring up the answers. I even looked at the tutorial i used and it said it was. I can think what else it would be. Is there any code like hints I should be looking for? Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1024747 Share on other sites More sharing options...
doctor-Eggman Posted March 11, 2010 Author Share Posted March 11, 2010 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1024947 Share on other sites More sharing options...
hcdarkmage Posted March 11, 2010 Share Posted March 11, 2010 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."; Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1024953 Share on other sites More sharing options...
doctor-Eggman Posted March 11, 2010 Author Share Posted March 11, 2010 You are really gonna hit me, but where do I add that in the code? Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1024956 Share on other sites More sharing options...
hcdarkmage Posted March 11, 2010 Share Posted March 11, 2010 You are really gonna hit me, but where do I add that in the code? That is a good question. My bets would be somewhere AFTER the score is tallied. Other then that, without seeing the whole thing, I wouldn't know where you'd put it. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1024960 Share on other sites More sharing options...
doctor-Eggman Posted March 12, 2010 Author Share Posted March 12, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1025140 Share on other sites More sharing options...
hcdarkmage Posted March 12, 2010 Share Posted March 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1025253 Share on other sites More sharing options...
doctor-Eggman Posted March 12, 2010 Author Share Posted March 12, 2010 oh that wasnt the maths from it, I did that awful maths in my head on the spot as an example haha. I finally got it displaying a percentage. unfortunately my leaderboard still shows it as something/20. Any clue how I can make the leaderboard show the percentage? Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1025304 Share on other sites More sharing options...
hcdarkmage Posted March 12, 2010 Share Posted March 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1025402 Share on other sites More sharing options...
doctor-Eggman Posted March 14, 2010 Author Share Posted March 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1025783 Share on other sites More sharing options...
hcdarkmage Posted March 15, 2010 Share Posted March 15, 2010 Post your changed code. It may not be resetting the percent values, so it might just be running a total percentage based on how many times you do the quiz. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1026402 Share on other sites More sharing options...
doctor-Eggman Posted March 15, 2010 Author Share Posted March 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1026430 Share on other sites More sharing options...
doctor-Eggman Posted March 15, 2010 Author Share Posted March 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1026579 Share on other sites More sharing options...
doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 Any idea mage? Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1027155 Share on other sites More sharing options...
hcdarkmage Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/#findComment-1027186 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.