Snooble Posted March 19, 2008 Share Posted March 19, 2008 Hey everyone, hopefully a quick one: $level = 2; $case = 1; $xpneeded = 50; switch ($list['level']) { case $case: if($list['xp'] >= $xpminus){ $xpchange = $list['xp'] - $xpneeded; $update = "UPDATE users SET level = ".$level.", xp = ".$xpchange." WHERE id = ".$list['id'].""; mysql_query($update); } break; $level = $level ++; $case = $case ++; $xpneeded = $xpneeded + ($xpneeded / 2); case $case: if($list['xp'] >= $xpminus){ $xpchange = $list['xp'] - $xpneeded; $update = "UPDATE users SET level = ".$level.", xp = ".$xpchange." WHERE id = ".$list['id'].""; mysql_query($update); } break; $level = $level ++; $case = $case ++; $xpneeded = $xpneeded + ($xpneeded / 2); case $case: if($list['xp'] >= $xpminus){ $xpchange = $list['xp'] - $xpneeded; $update = "UPDATE users SET level = ".$level.", xp = ".$xpchange." WHERE id = ".$list['id'].""; mysql_query($update); } break; Does it make sense to you lot? the problem is that the switch statements arent recognised and also the ++'s aren't recognised... where would i have to place them... ? Thanks Sam Quote Link to comment https://forums.phpfreaks.com/topic/96820-whys-the-switch-statement-not-working/ Share on other sites More sharing options...
teng84 Posted March 19, 2008 Share Posted March 19, 2008 your code is so confusing!!! can you tell us what do you want with that code plus you have the counter which i think is useless because you dont have a loop and why use variables in your cases!!!!! hmmm Quote Link to comment https://forums.phpfreaks.com/topic/96820-whys-the-switch-statement-not-working/#findComment-495485 Share on other sites More sharing options...
Snooble Posted March 19, 2008 Author Share Posted March 19, 2008 It's fine i ended up using the below script... (you're right about it being too confusing) $level = 0; $nextlevel = $list['level'] + 1; $xpneeded = 50; for($i=0; $i<$nextlevel; ++$i) { $level++; $xpneeded = $xpneeded + ($xpneeded / 5); } if($list['xp'] >= $xpneeded){ $updatexp = $list['xp'] - $xpneeded; $levelchange = "UPDATE users SET level = ".$level.", xp = ".$updatexp." WHERE id = ".$list['id'].""; mysql_query($levelchange) or die ("Couldn't execute $sql: " . mysql_error()); $grownlevel = 1; } Quote Link to comment https://forums.phpfreaks.com/topic/96820-whys-the-switch-statement-not-working/#findComment-495507 Share on other sites More sharing options...
Jeremysr Posted March 19, 2008 Share Posted March 19, 2008 I think you can only use literal values with case, like 'string' or 1, not a variable or expression. Quote Link to comment https://forums.phpfreaks.com/topic/96820-whys-the-switch-statement-not-working/#findComment-495546 Share on other sites More sharing options...
kenrbnsn Posted March 19, 2008 Share Posted March 19, 2008 No, you can put other things into a case, for example: <?php $x = rand(0,100); switch (true) { case ($x < 25): echo $x . ' is less than 25'; break; case ($x > 24 && $x <75): echo $x . ' is between 24 & 75'; break; default: echo $x . ' is greater than 74'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/96820-whys-the-switch-statement-not-working/#findComment-495555 Share on other sites More sharing options...
Jeremysr Posted March 19, 2008 Share Posted March 19, 2008 Oh I must have been thinking of C... Quote Link to comment https://forums.phpfreaks.com/topic/96820-whys-the-switch-statement-not-working/#findComment-495581 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.