Jump to content

else if tags.


MuphN

Recommended Posts

Hello I have a problem. I think I cant do that, how can I go arround this, because in javascript I guess u can go else if how meny times u want to, but in php it shows an error, and cant find the mistake is it else if's or something else thats causing the trubble?

function uXL($mysqli) {

	$lu2 = lvl2($mysqli);
	$lu3 = lvl3($mysqli);
	$lu4 = lvl4($mysqli);
	$lu5 = lvl5($mysqli);
	$lu6 = lvl6($mysqli);
	$lu7 = lvl7($mysqli);
	$lu8 = lvl8($mysqli);
	$lu9 = lvl9($mysqli);
	$lu10 = lvl10($mysqli);
	$xp = getXp($mysqli);
		$lvl = getLvl($mysqli);
		$nolvl = "You don't have enough experiance points!";
		$maxlvl = "You are allready maximum level!";

	if ($lvl == 1) {
		if($xp >= 200 ){
			$lu2;
		}else {
			echo $nolvl;
		}
	}else if ($lvl == 2) {
		if($xp >= 500 ){
			$lu3;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 3) {
		if($xp >= 1000 ){
			$lu4;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 4) {
		if($xp >= 1700 ){
			$lu5;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 5) {
		if($xp >= 2500 ){
			$lu6;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 6) {
		if($xp >= 3200 ){
			$lu7;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 7) {
		if($xp >= 4000 ){
			$lu8;
		}else {
			echo $nolvl;
		}
	}else if($lvl ==  {
		if($xp >= 5000 ){
			$lu9;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 9) {
		if($xp >= 6200 ){
			$lu10;
		}else {
			echo $nolvl;
		}
	}else{
		echo $maxlvl;
	}
}

Everything works good I tested it take the $lvl and takes the $xp but it always goes for if lvl 1 and lu2 nothing more, I did try to set in database the level 3 for testing further, but it jumped into first one again, doesnt matter witch one I set it always leads to lu2; for some reason.. Help me see the syntax mistake.

Link to comment
Share on other sites

 

 

I think I cant do that, how can I go arround this, because in javascript I guess u can go else if how meny times u want to

PHP has if/elseif/else statements just like Javascript.

 

 

but in php it shows an error, and cant find the mistake is it else if's or something else thats causing the trubble?

What is the error you are getting?

 

 

 

Everything works good I tested it take the $lvl and takes the $xp but it always goes for if lvl 1 and lu2 nothing more, I did try to set in database the level 3 for testing further, but it jumped into first one again, doesnt matter witch one I set it always leads to lu2; for some reason.. Help me see the syntax mistake.

What is the code for getLvl() function?

Edited by Ch0cu3r
Link to comment
Share on other sites

 

 

What is the code for getLvl() function?
function getLvl($mysqli) {
    if ($stmt = $mysqli->prepare("SELECT level FROM members WHERE id = ? LIMIT 1")) {
            $user_id = $_SESSION['user_id'];
            $stmt->bind_param('i', $user_id);
            $stmt->execute();
            $stmt->store_result();
            if ($stmt->num_rows == 1) {
                $stmt->bind_result( $lvl);
                $stmt->fetch();
                return $lvl;
			}
		}
	return false;
}

 

 

What is the error you are getting?

No error, just always goes for $ul2; nothing else, doesnt matter if you are lvl 3 or lvl 5

Link to comment
Share on other sites

How do you get the id for the SELECT before the prepare?

 

I dont get it before, it gets after it, it selects the ID by taking it from seesion

$user_id = $_SESSION['user_id']; - this sets the user_id to Session Id, so it takes the ID from ur current session.

The session starts when its set, its set on the page where u need to do stuff.

$stmt->bind_param('i', $user_id); - This sets the user_id for the ? one at the prepares. It prepers frists then sets the user id from session and then executes the query. If Im correct, ughh bad at explaining.

Link to comment
Share on other sites

some comments about the coding style you are showing in this code.

 

1) you need to use complete words for your functions and variables that describe what the function or variable is for. you might know what these cryptic names mean now, but a month from now, you won't when you need to quickly find a problem with the code. by using complete words, your code will become self-describing/self-documenting. this will both help you AND it will help someone who you might be asking for help in a forum.

 

2) it's likely your series of functions are getting different pieces of data for the current user? if so, you should just get all the data at once using one or as few queries as possible. the code you have shown with 11 different function calls, is 'killing' your database server. for a game script, this will end up limiting the number of simultaneous players to a small value (around 10 based on previous threads people have asked help with) and make your web host upset at you for using too many server resources.

 

3) your code contains stray variables on a line by themselves - $lu2; that doesn't actually do anything, which is possibly the cause of your symptom.

 

by doing these items, we will be able to tell by looking at your code, what it is trying to accomplish, so that we might be able to help you. as it is, we don't a clue what your cryptic logic is supposed to be doing.

Edited by mac_gyver
Link to comment
Share on other sites

Thank you for you response.

But how can I make it that it would call like two or even all rows from database and make it different I mean.

function getbase($mysqli) {
if ($stmt = $mysqli->prepare("SELECT level, xp, somthing, else, blah FROM members WHERE id = ? LIMIT 1")) {
$user_id = $_SESSION['user_id'];
$stmt->bind_param('i', $user_id);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 5) {
$stmt->bind_result( $s, $x, $b, $f, $g);
$stmt->fetch();
return $s, $x, $b, $f, $g;
            }
        }
    return false;
}

Should it be like that, Sorry Im kinda sleepy.

 

 

 

1) you need to use complete words for your functions and variables that describe what the function or variable is for. you might know what these cryptic names mean now, but a month from now, you won't when you need to quickly find a problem with the code. by using complete words, your code will become self-describing/self-documenting. this will both help you AND it will help someone who you might be asking for help in a forum.

 

Got it, makes sense.

 

 

 

3) your code contains stray variables on a line by themselves - $lu2; that doesn't actually do anything, which is possibly the cause of your symptom.

 

lu2,3,4.... actually does, it uploads some numbers to database, I guess I should use one querry somehow aswell?

function lvl2($mysqli) {
		if ($stmt = $mysqli->prepare("UPDATE members SET level = 2, xp = xp-200 WHERE id = ? LIMIT 1")) {
			$user_id = $_SESSION['user_id'];
			$stmt->bind_param('i', $user_id);
			if ($stmt->execute() && $mysqli->affected_rows > 0) {
				header('Location: index.php');
				exit;
			}
		}
	return false; 
}
--------------------
function lvl3($mysqli) {
		if ($stmt = $mysqli->prepare("UPDATE members SET level = 3, xp = xp-500 WHERE id = ? LIMIT 1")) {
			$user_id = $_SESSION['user_id'];
			$stmt->bind_param('i', $user_id);
			if ($stmt->execute() && $mysqli->affected_rows > 0) {
				header('Location: index.php');
				exit;
			}
		}
	return false; 
}
=--------------
and so on.

I added some lines to understand it more easy.

function uXL($mysqli) {

	$lu2 = lvl2($mysqli);
	$lu3 = lvl3($mysqli);
	$lu4 = lvl4($mysqli);
	$lu5 = lvl5($mysqli);
	$lu6 = lvl6($mysqli);
	$lu7 = lvl7($mysqli);
	$lu8 = lvl8($mysqli);
	$lu9 = lvl9($mysqli);
	$lu10 = lvl10($mysqli);
	$xp = getXp($mysqli);
		$lvl = getLvl($mysqli);
		$nolvl = "You don't have enough experiance points!";
		$maxlvl = "You are allready maximum level!";
		
//If level is 1
	if ($lvl == 1) {  
	//and xp is more or equal then 200
		if($xp >= 200 ){ 
		//does this function.
			$lu2; 
		}else {
		//If u dont have 200 xp points it says that u dont have it.
			echo $nolvl;
		}
	}else if ($lvl == 2) {
		if($xp >= 500 ){
			$lu3;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 3) {
		if($xp >= 1000 ){
			$lu4;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 4) {
		if($xp >= 1700 ){
			$lu5;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 5) {
		if($xp >= 2500 ){
			$lu6;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 6) {
		if($xp >= 3200 ){
			$lu7;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 7) {
		if($xp >= 4000 ){
			$lu8;
		}else {
			echo $nolvl;
		}
	}else if($lvl ==  {
		if($xp >= 5000 ){
			$lu9;
		}else {
			echo $nolvl;
		}
	}else if($lvl == 9) {
		if($xp >= 6200 ){
			$lu10;
		}else {
			echo $nolvl;
		}
	}else{ //level 10 or more or 0 shows that ur maximum level allready.
		echo $maxlvl;
	}
}

if(isset($_POST['lvlup'])) //If button is clicked, it does the function.
{
	if(uXL($mysqli))
	{
	//If the querry is sucsessifull it shows that u gained a level.
		echo "You have gained a level!";
	}
	else
	{
	// if Not, then not.
		echo "some prob!";
	}
}
Link to comment
Share on other sites

for my point #3, you are NOT calling functions, those are variable names you are using in your code $lu2; , they are not function calls, which would look like lvl2($mysqli);

 

and again, your code is not doing anything because of that. also, by calling all those functions up front, you are messing with the values stored in your database table.

Link to comment
Share on other sites

Where you have $lu2; it doesnt actually call the function you assigned to the variable earlier on. That function will be called immediately when you assigned it to a variable.

 

As the all the lvl functions do the same thng you should consider making them a generic function. Where you pass in the  level and XP values that needs to be updated

function updateUsersLevelXP($mysqli, $user_id, $level, $xp) {
    $result = $mysqli->query("UPDATE members SET level = $level, xp = $xp WHERE id = $id LIMIT 1"); // no need for a prepare query as you are setting the values, not the user
    if ($result && $mysqli->affected_rows > 0) {
       return true;
    }

    return false;
}

The uXL function is not needed, you'd place the code in that function after if(isset($_POST['lvlup']) {

if(isset($_POST['lvlup'])) //If button is clicked,
{
    $xp  = getXp($mysqli);   // get current XP
    $lvl = getLvl($mysqli);  // get current level

    // error messages
    $nolvl  = "You don't have enough experiance points!";
    $maxlvl = "You are allready maximum level!";

    $validXPLevel = true; // If user does not have valid XP it will be set to false

    if($lvl < 10) { //level 10 or more or 0 shows that ur maximum level allready.
        // now update xp based on level
        if ($lvl == 1 && $xp >= 200 ){ 
            $xp -= 200;
        }else if ($lvl == 2 && $xp >= 500 ){
            $xp -= 500;
        }else if($lvl == 3 && $xp >= 1000 ){
            $xp -= 1000;
        }else if($lvl == 4 && $xp >= 1700 ){
            $xp -= 1700;
        }else if($lvl == 5 && $xp >= 2500 ){
            $xp -= 2500;
        }else if($lvl == 6 && $xp >= 3200 ){
            $xp -= 3200;
        }else if($lvl == 7 && $xp >= 4000 ){
            $xp -= 4000;
        }else if($lvl == 8 && $xp >= 5000 ){
            $xp -= 5000;
        }else if($lvl == 9 && $xp >= 6200 ){
            $xp -= 6200;
        } else{
            $validXPLevel = false;
        }

        // if user still has valid xp, then update xp and level in database
        if($validXPLevel) {
            // pass in db, user id, level and xp values
            if(updateUsersLevelXP($mysqli, $_SESSION['user_id'], $lvl + 1, $xp)) {
                echo "You have gained a level!";
            } else {
                // if Not, then not.
                echo "some prob!";
            }
        } else {
            echo $nolvl; // not enough xp error
        }
    } else {
        echo $maxlvl; // max level error
    }
}
Edited by Ch0cu3r
Link to comment
Share on other sites

Unrelated, but pro tip. You can convert all those elseif()s into a switch() statement to clean up the code a bit. The case statements within a switch need to equal the value of the switch(). So, it is counter-intuitive to have a comparison for the case statement. But, this does work

 

 

switch(true)
{
    case ($lvl < 10):
        //Do something
        break;
    case ($lvl == 1 && $xp >= 200):
        //Do something
        break;
    case (lvl == 2 && $xp >= 500):
        //Do something
        break;
    //Etc.
}
Link to comment
Share on other sites

i'm fond of using data arrays to define values that generic code then uses so that you don't need to go in and find and edit the actual program logic just because you want to change some data points. this also leads to more code reuse/DRY since the processing is often the same, though the meaning of the variables change.

Link to comment
Share on other sites

 

how can I make it that it would call like two or even all rows from database and make it different I mean

The LIMIT 1 you have in the SELECT makes it only return 1 row

 

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

 

http://dev.mysql.com/doc/refman/5.0/en/select.html

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.