Jump to content

If Else Question


Usarjjaco

Recommended Posts

Hey guys;Using an IF Else statement that is pulling data from both a SQL database and a user input form.The user input form is 2 drop down select menus; one distance; one direction.Statements:$distance=$_POST["distance"]$direction=$_POST["direction"]// Variable to see if player has enough fuel.IF($fuel<$distance * .02) {Stuff } IF ($time<$distance * 2) {Stuff}Else { IF($distance==1) { IF($direction=='N'){}Else { IF($distance==2){ IF($direction=='N') {I run the else if for quite some time... the distance ranges from 1 - 30 and the directions are N S E W .... So it would be like 1 N , 1 S, 1 E, 1 W, 2 N, 2 S .... etc etc All using ELSE IF following the initial two if statements.The script executes fine if I pick any of the "1" values in the form. ... or any number that I choose so long as that number is the first set that I use in my ELSE IF for example: 2 N, 2 S, 2 E, 2 W, 1 N, 1 S <--- If I chose a 2 N in the form here it would work; if I choose any other number it would notwhere as: 1 N, 1 S, 1 E, 1 W, 2 N, 2 S <---- If I choose 1 N it works; if I choose any other number it does not work.So I know that the form is handing the right numbers to the action page. I'm not sure where I'm going wrong. It's not giving me an error when the script doesn't execute; just a blank screen with the background scheme and my css nav bar.Thanks in advance.JJ

Link to comment
Share on other sites

I would recommend you try writing these out on paper and get it to make sense there first, before trying to code it out. I'm pretty confused by what you're asking for from the script, so imagine how the computer feels. Remember when we suggested debugging your script in that other thread? It always applies for as long as you code. You need to put echo and print statements inside functions and after conditionals, and when the script starts, so you know what values you're starting out with and you know which conditionals have been carried out and in what order. You won't know for sure unless you have the computer tell you. Save yourself the guess and just debug.btw, this is how I see your code, which may or may not be the same way yours is formatted in your script, but it might help other people who want to help and for them it will probably make more sense when trying to read it.

if($fuel<$distance * .02) {  //Stuff};if($time<$distance * 2){  //Stuff}else{  if($distance==1) {	if($direction=='N'){	};  }else{	if($distance==2){	  if($direction=='N') {	  };	};  };};

Link to comment
Share on other sites

I'm gonna go ahead and throw a monkey wrench in here.You might be better to use a switch statement instead of all those if's

if($fuel<$distance * .02) {  //Stuff}if($time<$distance * 2){  //Stuff}else{  if($distance==1) {	switch ($direction) {	  case 'N':		//stuff		break;	  case 'S':		...	}  }else{	if($distance==2){	  switch ($direction) {		case 'N':		  //stuff		  break;		case 'S':		  ...	  }	}  }}

Switches are (at least in my opinion) easier to understand.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...