berry05 Posted December 16, 2008 Share Posted December 16, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/ Share on other sites More sharing options...
berry05 Posted December 16, 2008 Author Share Posted December 16, 2008 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-716434 Share on other sites More sharing options...
jin29neci Posted December 16, 2008 Share Posted December 16, 2008 do you mean nested if else statements here's the syntax if (condition){ if (condition) { statements; statements; } elseif ...... (condition) { statements; } else { statements; } } else { statements; } Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-716517 Share on other sites More sharing options...
waynew Posted December 16, 2008 Share Posted December 16, 2008 Use as many as them as you please. Just don't expect me to read your code afterwards. Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-716524 Share on other sites More sharing options...
berry05 Posted December 16, 2008 Author Share Posted December 16, 2008 i tried it twice...with editing the codes and i get a error saying that Parse error: syntax error, unexpected T_VARIABLE on line 17.. but this is line 17... $Row = mysql_fetch_array($Result ); Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-716625 Share on other sites More sharing options...
kenrbnsn Posted December 16, 2008 Share Posted December 16, 2008 What are a few lines before that one. An error like that is usually caused by a problem on a previous line that PHP doesn't catch until something else doesn't make sense. Ken Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-716641 Share on other sites More sharing options...
berry05 Posted December 16, 2008 Author Share Posted December 16, 2008 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'") are a few lines before that one Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-717070 Share on other sites More sharing options...
premiso Posted December 16, 2008 Share Posted December 16, 2008 $Result = mysql_query("SELECT gold FROM users WHERE username='$username'") Need a semi-colon after that. Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-717071 Share on other sites More sharing options...
dennismonsewicz Posted December 16, 2008 Share Posted December 16, 2008 you could simplify the process and use a switch Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-717075 Share on other sites More sharing options...
berry05 Posted December 16, 2008 Author Share Posted December 16, 2008 thxs premisco! that was the problem but i found another problem but the problem is because i did my elseif wrong....but i can figure that out.... and dennis i'll look into a switch! thxs guys! Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-717080 Share on other sites More sharing options...
dennismonsewicz Posted December 16, 2008 Share Posted December 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-717083 Share on other sites More sharing options...
berry05 Posted December 16, 2008 Author Share Posted December 16, 2008 thxs man! i've been looking at this too... http://www.tizag.com/phpT/switch.php that's a good tut right? Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-717084 Share on other sites More sharing options...
dennismonsewicz Posted December 16, 2008 Share Posted December 16, 2008 yeah thats a good one bud! Good luck with everything man! Quote Link to comment https://forums.phpfreaks.com/topic/137151-solved-if-else-statement-inside-of-another-if-else-statement/#findComment-717102 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.