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?

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'];


?>

do you mean nested if else statements

 

here's the syntax

 

 

if (condition){

 

    if (condition) {

        statements;

        statements;

        }

    elseif ...... (condition) {

      statements;

        }     

    else {

      statements;

        }     

}

else {

statements;

}

 

here is a good tutorial on switches

 

http://us3.php.net/manual/en/control-structures.switch.php

 

The one above shows you if else and a switch

 

also try this one

 

http://www.tizag.com/phpT/switch.php

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.