stevenrfc Posted July 21, 2012 Posted July 21, 2012 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. Quote
KyleMassacre Posted July 21, 2012 Posted July 21, 2012 I believe you would need to use an if/else statement cause the only time ive seen switches is for functions Quote
stevenrfc Posted July 21, 2012 Author Posted July 21, 2012 I believe you would need to use an if/else statement cause the only time ive seen switches is for functions Ah okay thanks :) Quote
a_bertrand Posted July 21, 2012 Posted July 21, 2012 Switch would work even for numbers, however it would pick the value only for perfect fits, so let's say you have 10 different numbers and 10 values in the switch, that would work. Not in your case. Quote
Spudinski Posted July 21, 2012 Posted July 21, 2012 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'; Quote
stevenrfc Posted July 21, 2012 Author Posted July 21, 2012 Ah okay thanks Looks quite complex for me atm, but im sure I'll get there :P Quote
KyleMassacre Posted July 22, 2012 Posted July 22, 2012 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 Quote
stevenrfc Posted July 22, 2012 Author Posted July 22, 2012 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 :) Quote
KyleMassacre Posted July 22, 2012 Posted July 22, 2012 can you maybe show what you did for viewuser and also what you attempted for the index? Quote
stevenrfc Posted July 23, 2012 Author Posted July 23, 2012 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' Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.