Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make ranking in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.09k
    Comment on it

    Hello Reader's If you are developing the project related to gaming and you want make the Ranking of your users, Then this blog is very helpful to you.

    Suppose you have a criteria of total wins by a user on which the ranking will assign. You can not make the mysql order by syntax because same number of wins count of multiple user should make their same ranking.

    Lets start building this ,

    Step 1  get the data from table:

      $data = [];
    
    $players = $this->PlayerProfile->find('all',array('fields'=>array('player_id','winsCounter')));
    
    foreach ($players as $player) {
    $ranking[$player['PlayerProfile']['player_id']] = $player['PlayerProfile']['winsCounter'];
    		}

    The result get in $ranking will be a un ordered list so we have to order it first

    $rankings = array_unique($ranking);
    rsort($rankings);
    

    Now we'll make the ranking according the count of wins by every user. And its code will go like this:-

    		// now just use the original array and lookup the rankings for each value
    		$return = array();
    		foreach($ranking as $key=>$value)
    		{
    			$rankedValue = array();
    			$rankedValue['id'] = $key;
    			$rankedValue['value'] = $value;
    			$rankedValue['rank'] = array_search($value, $rankings) + 1;
    			$return[] = $rankedValue;
    		}
    		
    		foreach($return as $getPlayerRank)
    		{
    			if($getPlayerRank['id'] == $userId)
    			{
    				$data["rank"] = $getPlayerRank['rank'];
    			}
    		}

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: