Jump to content
MakeWebGames

Switch statement - Help please


stevenrfc

Recommended Posts

Hello,

Im new to php and i need some guidance.

Here is a switch statement that I created

 

       switch ($stexp) {

 case 1000000:
 echo 'Strong';
 break;

 case 750000:
 echo 'Strong';
 break;

 case 500000:
 echo 'Strong';
 break;

 case 250000:
 echo 'Strong';
 break;

 case 100000:
 echo 'Strong';
 break;

 case 75000:
 echo 'Average';
 break;

 case 20000:
 echo 'Weak';
 break;

 default:
 echo 'Feeble';
 break;


 }

 

If i created a Switch statement like this it wouldnt work the i wanted it.

Is there anyway to make a switch statement say If greater than X then echo rank name. Or can you only assign single values like i have above??

Because atm if i had over 20k exp then it would still say im feeble, I would have to get i exactly on 20k to get the name Weak showing.

Would I have to use an If else statement instead??

Thank you.

Link to comment
Share on other sites

I would say a switch is over-rated here.

As I see it, all your numbers are dividable by 1,000 without having fractions: so use an indexed array.

E.g.:

// array containing a reference map to text
// keys doesn't have to be strings, int/double will also work.
$exp_vals =  array(
   '1000' => 'Strong',
   ...
   '75' => 'Average',
   '20' => 'Weak'
);

if (!($exp % 1000) && array_key_exists($exp, $exp_vals))
   echo $exp_vals[$exp];
else
   echo 'Feeble';
Link to comment
Share on other sites

Ah okay thanks

Looks quite complex for me atm, but im sure I'll get there :P

Yeah it looks a little complex but he pretty much dis all the dirty work for you. The only thing it looks like you need to do is just fill in the blanks or the gaps.

I have a question though, is this for a particular engine cause if it is and its mccode or you are familiar with mccode it looks kinda like you may be able to use the forum ranking function cause if im reading spuds code right it looks different but I assume its pretty much functions the same

Link to comment
Share on other sites

Yeah its for mccode, but i just did it like this and it works sort of, but only on Viewuser.php it doesnt seem to be working on index.php lol

 


if ($stexp>=1000000) {
 echo ' Strong';
} elseif ($stexp>=750000) {
 echo ' Strong';
} elseif ($stexp>=500000) {
 echo ' Strong';
} elseif ($stexp>=250000) {
 echo ' Strong';
} elseif ($stexp>=100000) {
 echo ' Strong';
} elseif ($stexp>=75000) {
 echo ' Average';
} elseif ($stexp>=20000) {
 echo ' <font color="#B22222">Weak</font>';
} else {
 echo ' <font color="red">Feeble</font>';
}

 

Thanks :)

Link to comment
Share on other sites

Okay i have got that same thing in both Viewuser and index, I have this

 

$ts = $ir['strength'] + $ir['agility'] + $ir['guard'] + $ir['labour']
               + $ir['IQ'];
$ir['strank'] = get_rank($ir['strength'], 'strength');
$ir['agirank'] = get_rank($ir['agility'], 'agility');
$ir['guarank'] = get_rank($ir['guard'], 'guard');
$ir['labrank'] = get_rank($ir['labour'], 'labour');
$ir['IQrank'] = get_rank($ir['IQ'], 'IQ');
$tsrank = get_rank($ts, 'strength+agility+guard+labour+IQ');
$ir['strength'] = number_format($ir['strength']);
$ir['agility'] = number_format($ir['agility']);
$ir['guard'] = number_format($ir['guard']);
$ir['labour'] = number_format($ir['labour']);
$ir['IQ'] = number_format($ir['IQ']);
$ts = number_format($ts);
.
.
.
.

       if ($ts>=1000000) {
 echo ' Strong';
} elseif ($ts>=750000) {
 echo ' Strong';
} elseif ($ts>=500000) {
 echo ' Strong';
} elseif ($ts>=250000) {
 echo ' Strong';
} elseif ($ts>=100000) {
 echo ' Strong';
} elseif ($ts>=75000) {
 echo ' Average';
} elseif ($ts>=20000) {
 echo ' <font color="#B22222">Weak</font>';
} else {
 echo ' <font color="red">Feeble</font>';
}

 

I have the exact same thing on both pages but for some reason it only works on Viewuser.

I have +20k and on View user it says im 'Weak' but on my index page it says that i am still 'Feeble

Thanks'

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...