Jump to content

Whys the switch statement not working?


Snooble

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/96820-whys-the-switch-statement-not-working/
Share on other sites

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;
}

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';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.