Jump to content

[SOLVED] if else statement inside of another if else statement?


berry05

Recommended Posts

is there a way i can put a if else statement inside of another if else statement?

because when a user buys a hoe "and I'm talking about farming" i want the code to check if the user already has one and i want it to check if they have enough gold...then once that part is done it either buys the hoe or says sorry blah blah blah blah!

 

is there a way i can do that?

Link to comment
Share on other sites

heres my code...forgot to post it..

 

<?php
session_start();
if(isset($_SESSION['username'])){
?>


<p></p>
<p><a href="shop.php">Shop</a> </p>
<?php

//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("textgame") or die(mysql_error());

//check if hoe is already bought by that user
$username = $_SESSION['username'];
$query = "SELECT * FROM users_items WHERE username='$username' AND item = 'hoe'";
$result = mysql_query($query) or die( mysql_error());
if($row = mysql_fetch_row($result)){
echo "You only need to buy one hoe!!";
	}else{
	// check if user has enough gold
	$Result = mysql_query("SELECT gold FROM users WHERE username='$username'")
	$Row = mysql_fetch_array($Result );
	if ($Row < 50) }
	echo "You dont have enough gold to purchase a hoe!!";

	}
	else
	{
	// start adding data
	mysql_query("INSERT INTO users_items (username, item) VALUES('$username', 'hoe' ) ") 
	or die(mysql_error());
	echo "you have bought a hoe!!";
}
}

//session_register("inventory");
$Query = "SELECT item FROM users_items WHERE username='$username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['item'];


?>

Link to comment
Share on other sites

do you mean nested if else statements

 

here's the syntax

 

 

if (condition){

 

    if (condition) {

        statements;

        statements;

        }

    elseif ...... (condition) {

      statements;

        }     

    else {

      statements;

        }     

}

else {

statements;

}

 

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.