Jump to content

Recommended Posts

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;
}

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.

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.