Complex If Statement[resolved]

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Complex If Statement[resolved]

Post by 62896dude »

Hey there,

After a long day of coding, I produced a rather complex if statement, but I am having a problem. Here is how it works:

There is an html form with radio button selection. Then, on the next page, it runs if statements for every possible combination of choices to award the person the appropriate wand. So here is my problem, no matter what options I choose, I always get the first option listed, which says

Code: Select all

Olivander notes all of your answers and finally looks up at you and says <i>&#34Ah, yes, it seems that I know just the wand for you. Here it is, a <font color='red'>10&#34 Hawthorn Wand with a Unicorn Hair core</font>. You know, Draco Malfoy has this very same wand!&#34</i>";
Here is wand1.php, this is where you answer the questions from the radio buttons:

Code: Select all

<html>
<head>
<title>Weasleys' Wizard Wheezes</title>
</head>
<body>

<link rel="stylesheet" href="style.css" type="text/css" />
<div id="index" align="center">

<?php
include_once 'connect.php';
session_start();

include_once 'logo.php';
?>
 <link href="style.css" rel="stylesheet" type="text/css" />
<div id="login2" div align="center">


<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='home.php'>Login</a>";
  exit;
}
?>
</div>

<?php
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
?>
<div align:center; width:60%;>
<br><br><br><br>
<form method="GET" action="wandselect.php">
<b>Question 1:</b>
<br><br>
How would you describe yourself?<br><br>
Selfish:<input type="radio" value="Selfish" name="Describe"><br>
Brave:<input type="radio" value="Brave" name="Describe"><br><br>
<b>Question 2:</b>
<br><br>
What would you do if your friend was being tortured by a Death Eater?<br><br>
Join in with the Death Eater and have a good laugh about it:<input type="radio" value="Join in with the Death Eater and have a good laugh about it" name="Help"><br>
Defend your friend and tell off the Death Eater:<input type="radio" value="Defend your friend and tell off the Death Eater" name="Help"><br><br>
<b>Question 3:</b>
<br><br>
Which creature do you find most interesting?<br><br>
Unicorn:<input type="radio" value="Unicorn" name="Core"><br>
Dragon:<input type="radio" value="Dragon" name="Core"><br>
<br><br>
<input type="submit" value="Claim your wand!" name="submit"></form>
</div>
</body>
</html>
And here is wandselect.php, this is where the actual if statements are run:

Code: Select all

<html>
<head>
<title></title>
</head>
<body>

<link rel="stylesheet" href="style.css" type="text/css" />
<div id="index" align="center">

<?php
include_once 'connect.php';
session_start();

include_once 'logo.php';
?>
 <link href="style.css" rel="stylesheet" type="text/css" />
<div id="login2" div align="center">


<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='home.php'>Login</a>";
  exit;
}
?>
</div>

<?php
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);

$describe=$_GET['Describe'];
$help=$_GET['Help'];
$core=$_GET['Core'];

if ($describe="Selfish"){

	if ($help="Join in with the Death Eater and have a good laugh about it"){
	
		if ($core="Unicorn"){
		
		echo "Olivander notes all of your answers and finally looks up at you and says <i>&#34Ah, yes, it seems that I know just the wand for you. Here it is, a <font color='red'>10&#34 Hawthorn Wand with a Unicorn Hair core</font>. You know, Draco Malfoy has this very same wand!&#34</i>";
		
		}
		
		else{
		
		echo "Olivander notes all of your answers and finally looks up at you and says <i>&#34Ah, yes, it seems that I know just the wand for you. Here it is, a <font color='red'>12 3/4&#34 Walnut Wand with a Dragon Heartstring core</font>. You know, Bellatrix Lestrange has this very same wand!&#34</i>";
		
		}
	}
	
	else{
	
		if ($core="Unicorn"){
		
		echo "Olivander notes all of your answers and finally looks up at you and says <i>&#34Ah, yes, it seems that I know just the wand for you. Here it is, a <font color='red'>12 1/4&#34 Ash Wand with a Unicorn Hair core</font>. You know, Cedric Diggory has this very same wand!&#34</i>";
		
		}
		
		else{
		
		echo "Olivander notes all of your answers and finally looks up at you and says <i>&#34Ah, yes, it seems that I know just the wand for you. Here it is, a <font color='red'>10 1/4&#34 Hornbeam Wand with a Dragon Heartstring core</font>. You know, Victor Krum has this very same wand!&#34</i>";
		
		}
	
	}
}

else{

	if ($help="Join in with the Death Eater and have a good laugh about it"){
	
		if ($core="Unicorn"){
		
		echo "Olivander notes all of your answers and finally looks up at you and says <i>&#34Ah, yes, it seems that I know just the wand for you. Here it is, a <font color='red'>10 3/4&#34 Oak Wand with a Unicorn Hair core</font>. You know, Narcissa Malfoy has this very same wand!&#34</i>";
		
		}
		
		else{
		
		echo "Olivander notes all of your answers and finally looks up at you and says <i>&#34Ah, yes, it seems that I know just the wand for you. Here it is, an <font color='red'>18&#34 Elm Wand with a Dragon Heartstring core</font>. You know, Lucius Malfoy has this very same wand!&#34</i>";
		
		}
	
	}
	
	else{
	
		if ($core="Unicorn"){
		
		echo "Olivander notes all of your answers and finally looks up at you and says <i>&#34Ah, yes, it seems that I know just the wand for you. Here it is, a <font color='red'>14&#34 Willow Wand with a Unicorn Hair core</font>. You know, Ron Weasley has this very same wand!&#34</i>";
		
		}
		
		else{
		
		echo "Olivander notes all of your answers and finally looks up at you and says <i>&#34Ah, yes, it seems that I know just the wand for you. Here it is, a <font color='red'>10 3/4&#34 Vine Wand with a Dragon Heartstring core</font>. You know, Hermione Granger has this very same wand!&#34</i>";
		
		}
	
	}

}
?>
I am not sure if maybe there are some syntactical errors, but could someone please review this and share any ideas as to why I always get the first option? Thanks!
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Complex If Statement

Post by Nexus »

I figured it out!

it needs to be like this

Code: Select all

if($describe) {
     if($help) {
            if($core) {
with every single possible choice possibilty and the outcome
you only have the describe set to selfish but if someone picks brave then nothing is going to happen! Those else statements really don't do anything. I am working on it and will be uploading it to our host soon!
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: Complex If Statement

Post by 62896dude »

Yeah, but isnt that what my else statement was for?
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Complex If Statement

Post by Nexus »

I think but apparently it isn't working so I will go through and make them all if I just need you on skype to tell me some info
Loopy

Re: Complex If Statement

Post by Loopy »

You're always hitting the first option because you're assigning a value to $describe, and you want to be testing for equality. So when PHP hits that first statement, it says 'if I can assign 'Selfish' to the variable $describe, execute this code'... and of course it can always do this, so it always executes that first condition. What you want PHP to say is, 'if $describe is equal to 'Selfish' execute this code'.

In other words, you have this:
if ($describe="Selfish"){


What you want is this:
if ($describe=="Selfish"){

If you want to say 'if x is equal to y', then you have to use two equal signs, which means it's testing to see if things are equal, rather than assigning a value.
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: Complex If Statement

Post by 62896dude »

Oh my gosh, thank you SO much! Hours I spent trying different things on this code, only to have all of them fail, and thanks to you, it works! :)
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Complex If Statement

Post by Jackolantern »

We can all blame C for that horrendous design decision to allow non-Boolean statements in conditional logic :( Languages such as Java and C# remove that ability, only allowing statements that directly produce Boolean values, and it basically eradicates these types of errors, which are dreadfully common in languages such as C++, PHP, Javascript, ActionScript, etc.
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”