pengu Posted February 11, 2010 Share Posted February 11, 2010 2 Files. functions.inc.php -> <?PHP Function myLevel($my_xp) { switch($my_xp) { case ($my_xp > 500): // Level 2 $stuff = 'happens'; $my_level = 1; case ($my_xp > 1000): // Level 3 $stuff = 'happens'; case ($my_xp > 2000): // Level 4 $stuff = 'happens'; $my_level = 4; case ($my_xp > 3500): // Level 5 $stuff = 'happens'; case ($my_xp > 5500): // Level 6 $stuff = 'happens'; } } ?> stats.php <?PHP $my_xp = 2002; include('functions.inc.php'); myLevel($my_xp); echo $my_level; ?> Alrighty a few questions. I have removed the 'break;' from the switch, not sure if it's needed, but I thought it'd see that the first case was TRUE and break away and not bother going any further. Is this correct? How does switch handle when multiple things are true, will it take the last case down the list and use all the information from that, keeping in mind I'll be repeating certain information. (by info I mean variables) Finally It's not actually working, haha. So I want to include that list of functions, more will be added and I assume I am calling it correctly. I want to include it, set '$my_xp' then use the function to check the $my_xp and adjust information accordingly. Regards, Pengu. P.S It may look like a bad idea to do each level individually but there is method to my madness, method I tell you! Quote Link to comment Share on other sites More sharing options...
xjake88x Posted February 11, 2010 Share Posted February 11, 2010 Switch won't work for what you're trying to do at that point. Here is a simpler approach: function myLevel($my_xp) { global $my_level, $stuff; /*the above references the $my_level variable out of the function's scope without it, $my_level will be a separate variable specific only to this function in this scope (when it is being run this specific time*/ $xp_table = array( array('level' => 0, 'req_xp' => 0), array('level' => 1, 'req_xp' => 500), array('level' => 2, 'req_xp' => 1000), array('level' => 3, 'req_xp' => 2000), array('level' => 4, 'req_xp' => 3500), array('level' => 5, 'req_xp' => 5500) ); //loop backwards for($i = count($xp_table)-1; $i >= 0; $i++) { if($my_xp >= $xp_table[i]['req_xp']) { $my_level = $xp_table[i]['level']; break; } } //now switch switch($my_level) { case 0: $stuff = 'happens'; break; case 1: $stuff = 'happens'; break; case 2: $stuff = 'happens'; break; } //possible return value } Quote Link to comment Share on other sites More sharing options...
xjake88x Posted February 12, 2010 Share Posted February 12, 2010 It won't let me edit that one but here is a cleaner version function myLevel($my_xp) { global $my_level, $stuff; /*the above references the $my_level variable out of the function's scope. without it, $my_level will be a separate variable specific only to this function in this scope (when it is being run this specific time) */ $xp_table = array( array('level' => 0, 'req_xp' => 0), array('level' => 1, 'req_xp' => 500), array('level' => 2, 'req_xp' => 1000), array('level' => 3, 'req_xp' => 2000), array('level' => 4, 'req_xp' => 3500), array('level' => 5, 'req_xp' => 5500) ); //loop backwards for($i = count($xp_table)-1; $i >= 0; $i++) { //check if their XP matches the XP req of each level starting with the highest if($my_xp >= $xp_table[i]['req_xp']) { $my_level = $xp_table[i]['level']; break; //stop looping if we found the level } } //now switch between level which was already determined switch($my_level) { case 0: $stuff = 'happens'; break; case 1: $stuff = 'happens'; break; case 2: $stuff = 'happens'; break; } //possible return value } Quote Link to comment Share on other sites More sharing options...
pengu Posted February 12, 2010 Author Share Posted February 12, 2010 Thank you Jake. Next question is they will start at level 1.. so I pretty much want to count from 2 onwards, will this have any effect? I mean I can easily get around it by making there starting $my_xp at 500. Edit: On second glance, Global is what I was missing, thanks heaps Jake. Quote Link to comment Share on other sites More sharing options...
xjake88x Posted February 12, 2010 Share Posted February 12, 2010 No problem, somehow you can change this to be "resolved" but it's a relatively new feature I don't know how it works. Quote Link to comment Share on other sites More sharing options...
pengu Posted February 12, 2010 Author Share Posted February 12, 2010 Someone did it for me If you scroll down the bottom of the thread, it has a green solved button Quote Link to comment Share on other sites More sharing options...
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.