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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

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

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.