doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 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; //---Changed $score to percent $leaders[$name] = $percent; //---again $score to percent } arsort($leaders,SORT_NUMERIC); $counter = 1; $output = "<ul class=\"leaders\">\n"; foreach ($leaders as $key => $percent) { //---changed $score to percent // Check that $counter is less than $limit. if ($counter <= $limit) { if ($key == $_SESSION['user']) { $output .= "<li><strong>$key:</strong> $percent%</li>\n"; //--changed $value to $percent } else { $output .= "<li>$key: $percent%</li>\n"; //--- again $value to $percent } 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; I pretty much just changed all the $score and $value to percent here is how it was originally. 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; } Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/page/2/#findComment-1027197 Share on other sites More sharing options...
hcdarkmage Posted March 16, 2010 Share Posted March 16, 2010 I think I may see the problem. Or at least A problem: foreach ($leaders as $key => $percent) { //---changed $score to percent // Check that $counter is less than $limit. if ($counter <= $limit) { if ($key == $_SESSION['user']) { $output .= "<li><strong>$key:</strong> $percent%</li>\n"; //--changed $value to $percent } else { $output .= "<li>$key: $percent%</li>\n"; //--- again $value to $percent } if ($group) { // Use the modulus operator(%) to create new sub-list. if($counter % $group == 0) { $output .= "</ul>\n<ul class=\"leaders\">\n"; } } } $counter++; } You should have left most of this alone. It should read something like: foreach ($leaders as $key => $value) { if ($counter <= $limit) { if ($key == $_SESSION['user']) { $output .= "<li><strong>$key:</strong> $value \%</li>\n"; } else { $output .= "<li>$key: $value \%</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++; } Because it is calling forth an array: $leaders = array(); You have already changed the score variable to percent. foreach($xml->user as $user) { $name = (string)$user->name; $percent = (string)$user->score; //---Changed $score to percent $leaders[$name] = $percent; //---again $score to percent } But, the big question here is: Where are you getting this variable $percent = (string)$user->score; //<--- this $user->score here We find where that is being set and we can find what is going on with your percentages. Find the code that is setting that variable. Link to comment https://forums.phpfreaks.com/topic/194428-looking-to-create-an-online-quiz/page/2/#findComment-1027226 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.