0

I have a function like this

function cp_module_ranks_getRank($uid){
        return cp_module_ranks_pointsToRank(cp_getPoints($uid));
    }

It returns the member rank based on points i set. For example my settings has

  1. 250 Points = Bronze
  2. 1000 Points = Silver
  3. 2500 Points = Gold
  4. 10000 Points = Diamond
  5. 25000 Points = Platinum

So if a user has 11234 points then that function return value as Diamond

Now i would like to set a variable based on this rank.

$affdays is the variable

I mean like this

If user rank is Bronze $affdays = 30
If user rank is Silver $affdays = 60
If user rank is Gold $affdays = 90
If user rank is Diamond $affdays = 180
If user rank is Platinum $affdays = 360
Default $affdays = 10

Thanks.

1
  • why dont you use array ? Commented Mar 19, 2012 at 20:41

4 Answers 4

4

You can use a switch statement:

$user_rank = cp_module_ranks_getRank($id);

switch($user_rank) {
    case 'Bronze':
      $affdays = 30;
      break;
    case 'Silver':
      $affdays = 60;
      break;
    case 'Gold':
      $affdays = 90;
      break;
    case 'Diamond':
      $affdays = 180;
      break;
    case 'Platinum':
      $affdays = 360;
      break;
    default:
      $affdays = 10;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi thanks.. But my main problem is that i have no idea how to bring that function cp_module_ranks_getRank($uid) inside switch statement. Do i have to assign your $user_rank variable in that function?. If yes how?
I'm not sure what you actually want. Do you want the cp_module_ranks_getRank() function to return that $affdays value?
No.. cp_module_ranks_getRank() returns rank value like Bronze which is correct. But you used a new variable $user_rank. I'm a php noob. Thats why i asked this question. But now your new variable confusing me more.. I mean from where you passing the values?. Your code has no connection with my cp_module_ranks_getRank() function.
Yep thats what i'm talking about. Thanks. I'll accept your answer. :)
4

How about just creating an array and looking up the corresponding example?

$rank = 'Bronze';
$rank_days = array(
  'Bronze' => 30,
  'Silver' => 60
  // etc
);

if (array_key_exists($rank, $rank_days)) {
  $affdays = $rank_days[$rank]; // 30
} else {
  $affdays = 10;
}

3 Comments

+1 this is much better than multiple ifs or a switch statement
Hi can you tell me how to assign that function to a variable? Do i have to use like this? $rank = cp_module_ranks_getRank($uid); and what about the default affdays value?
That looks ok. I'm just assigning the ranks as the keys of an array and the number of days as the corresponding value; why not test if no corresponding key exists in the array using array_key_exists() and assign a default value if needed?
2

Use the switch statement

switch ($user_rank) {
   case "bronze":
      //code
   break;
   case "silver":
      //code
   break;
   default:
      $affdays = 10;
   break;
   //and so on
}

it's like an If statement with multiple "elseif's" and it works much faster

Comments

1

The following code should do the trick:

    if (cp_module_ranks_getRank($user) === 'Bronze') {
        $affdays = 30;
    } else if (cp_module_ranks_getRank($user) === 'Silver') {
        $affdays = 60;
    } else if (cp_module_ranks_getRank($user) === 'Gold') {

    ...

    } else {
        $affdays = 10;
    }

EDIT: The switch statement answer is far better than this one

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.