I have been currently working on a PHP based test where the results go to a leaderboard. At present the test tallys up the score as an out of 20 result “14/20” etc and sends it to a leaderboard via storage in a xml. I got some code that can make the percentage but it only displays it under the out of 20 results
e.g “you got 14/20 your percentage is 25% etc” What I am looking to do is have it so only the percentage is displayed and that it is the percentage that goes to the leaderboards rather than the mark out of 20.
I will provide the code below any and all help would be greatly greatly appreciated.
This is the code in the test part that I belive will send it to the leaderboard and the one where the result is initially displayed.
<?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>\
<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>\
";
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>\
";
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>\
";
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\\">\
";
// 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>\
";
} else {
$output .= "<li>$key: $value/20</li>\
";
}
// 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>\
<ul class=\\"leaders\\">\
";
}
}
}
// Increment the $counter.
$counter++;
}
// End the ordered list.
$output .= "</ul>\
";
// Print out the ordered list.
echo $output;
}
If anyone can give me any help with this I would be so grateful. Thank you so much for any and all help!