Jump to content

If statement


unplugged_web

Recommended Posts

I've spent day's trying to get this work, but I'm not getting anywhere so I thought I'd post here.What I'm trying to do is when somebody's filled out a form I want to check what they've said and then send them to a page depending on their results. So for example if they say 1 and 2 they go to one page, but if they say 1 and 1 they get another page. This is what I've got so far:

<?php$con = mysql_connect("xxxxxxx","xxxxxxx","xxxxxxx");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("database", $con);$code = $_GET['code'];$code2 = substr($_GET['code2'],0,1); // gets the first character..$result = mysql_query("SELECT * FROM telephone WHERE code1 = '$code1' AND code2 LIKE '$code2'");if ($code!=0207 ||  $code!=0208 || $code!=0121 || $code!=0151 || $code!=0845 || $code!=0844 || $code!=0870 || $code!=0131 || $code!=0500 || $code!=0300 || $code!=07 || $code!=03) {header("Location: pageone.php");} if ($code!=001 ||  $code!=0044 || $code!=0043 || $code!=0055 || $code!=0063) {header("Location: pagetwo.php");} else {header("Location: sorry.php");}?>

Thank

Link to comment
Share on other sites

Check the variables - there are three $code variables, and it looks like $code1 isn't initialized.The $_GETs are 'code' and 'code2'Also - 010 actually means octal 10 - or 8 (http://www.php.net/manual/en/language.types.integer.php)I'd change the if statements to use in_array with strings, for example:if (in_array($code,array('010','090' ...)))Also, it doesn't look like you're using the result from the database. http://us2.php.net/manual/en/function.mysql-query.php, you'd need to do a fetch.

Link to comment
Share on other sites

is it not working?it might be easier to read if you check for equality. maybe you could write the if statement like this:

if(code == (### || ### || ### || etc){  //stuff}else if{code == (### || ### || ###){  //some other stuff}else{  //last chance};

Link to comment
Share on other sites

Check the variables - there are three $code variables, and it looks like $code1 isn't initialized.The $_GETs are 'code' and 'code2'Also - 010 actually means octal 10 - or 8 (http://www.php.net/manual/en/language.types.integer.php)I'd change the if statements to use in_array with strings, for example:if (in_array($code,array('010','090' ...)))Also, it doesn't look like you're using the result from the database. http://us2.php.net/manual/en/function.mysql-query.php, you'd need to do a fetch.
I can only see two variables - although I've probably missed something. Also When I get to the next page the url contains the variables from both $code1 and $code2 so does that mean they are initialized?I did try changing the if statement so they're all in quotes, but that didn't work - although I don't know much about in_array so wasn't sure what you meant about putting it in a string?
Link to comment
Share on other sites

is it not working?it might be easier to read if you check for equality. maybe you could write the if statement like this:
if(code == (### || ### || ### || etc){  //stuff}else if{code == (### || ### || ###){  //some other stuff}else{  //last chance};

I tried that, but just got an error saying
Parse error: syntax error, unexpected '{' in E:\domains\m\domain.com\user\htdocs\postcode_test.php on line 13

line 13 is

if ($code!= ('0207' ||  '0208' || '0121' || '0151' || '0845' || '0844' || '0870' || '0131' || '0500' || '0300' || '07' || '03') {

Link to comment
Share on other sites

it's very likely the error is coming from the previous line, as you're using an uninitialized variable (code1) in your SQL statement:

$result = mysql_query("SELECT * FROM telephone WHERE code1 = '$code1' AND code2 LIKE '$code2'");

(as wirehopper pointed out)

Link to comment
Share on other sites

it's very likely the error is coming from the previous line, as you're using an uninitialized variable (code1) in your SQL statement:
$result = mysql_query("SELECT * FROM telephone WHERE code1 = '$code1' AND code2 LIKE '$code2'");

(as wirehopper pointed out)

In that that case then I'm really lost because I don't understand what you mean about uninitialized. :) It's sending the answer to the next page (the page with the code) but I guess that's no the same thing?
Link to comment
Share on other sites

I tried that, but just got an error saying
Parse error: syntax error, unexpected '{' in E:\domains\m\melovesushi.com\user\htdocs\postcode_test.php on line 13

line 13 is

if ($code!= ('0207' ||  '0208' || '0121' || '0151' || '0845' || '0844' || '0870' || '0131' || '0500' || '0300' || '07' || '03') {

You're missing a paren ')'if ($code!= ('0207' || '0208' || '0121' || '0151' || '0845' || '0844' || '0870' || '0131' || '0500' || '0300' || '07' || '03')) {
Link to comment
Share on other sites

In that that case then I'm really lost because I don't understand what you mean about uninitialized. :) It's sending the answer to the next page (the page with the code) but I guess that's no the same thing?
well, technically I guess it might not matter, since you aren't using $result anywhere in the script. Initializing is giving a variable a value at the same time you define it. So when you write $code = $_GET['code']; you are defining the variable $code1 and initializing it to the value of $_GET['code']. However in your script you haven't defined any such variable $code1, let along assign it a value, which should mean that your $result won't be what you expect it to be. But it looks like the paren is where the problem is at.
Link to comment
Share on other sites

well, technically I guess it might not matter, since you aren't using $result anywhere in the script. Initializing is giving a variable a value at the same time you define it. So when you write $code = $_GET['code']; you are defining the variable $code1 and initializing it to the value of $_GET['code']. However in your script you haven't defined any such variable $code1, let along assign it a value, which should mean that your $result won't be what you expect it to be. But it looks like the paren is where the problem is at.
Okay I've removed the $results, but I realized I didn't even need it :) although it's still not working. The code is now:
<?php$code = $_REQUEST['code'];$code2 = substr($_REQUEST['code2'],0,1); // gets the first character.if ($code!= ('0207' ||  '0208' || '0121' || '0151' || '0845' || '0844' || '0870' || '0131' || '0500' || '0300' || '07' || '03')) {echo("one");} else if { $code!= ('0207' ||  '0208' || '0121' || '0151' || '0845' || '0844' || '0870' || '0131' || '0500' || '0300' || '07' || '03') {echo("two4");} else {echo("sorry");?>

Link to comment
Share on other sites

your else if is starting with a curly brace instead of a parenedit: and you're missing the closing paren on the same line
:) I'm still getting the same error for line :
unexpected T_CONSTANT_ENCAPSED_STRING

Line 4 is now:

if ($code!= ('0207' ||  '0208' || '0121' || '0151' || '0845' || '0844' || '0870' || '0131' || '0500' || '0300' || '07' || '03')) {

I don't think it's even getting to the other line

Link to comment
Share on other sites

worth a shot, see if changing your echo statements to just echo "text here"; makes a differenceedit: also could you just re-post all your changes. It's worth checking to make sure you actually have a value for $code when you do the assignment from $_REQUEST

Link to comment
Share on other sites

worth a shot, see if changing your echo statements to just echo "text here"; makes a differenceedit: also could you just re-post all your changes. It's worth checking to make sure you actually have a value for $code when you do the assignment from $_REQUEST
Okay changing it to "text here" didn't make a difference, but hte code is now:
<?php$code = $_REQUEST['code'];$code2 = substr($_REQUEST['code2'],0,1); // gets the first character.if ($code!= ('SW1W' || 'SW3' || 'SW5' || 'SW6' || 'SW10' || 'SW11' || 'SW15' || 'SW18' || 'W6' || 'W14' || 'W6' || 'W14')){echo("text here");} else if ($code== ('SW1W' || ' SW3' || 'SW5' || 'SW6' || 'SW10' || 'SW11' || 'SW15' || 'SW18' || 'W6' || 'W14' || 'W6' || 'W14')) {echo("text here");} else {echo("text here")?>

Link to comment
Share on other sites

no. the point was to drop the parenthesis.edit: and for you to echo out the value of $code to make there is indeed a value to be checking against.edit edit: try leaving a space between $code and !=

Link to comment
Share on other sites

That if statement isn't correct, guys. Look at the order of operations. Look at what this reduces down to:if ($code!= ('SW1W' || 'SW3' || 'SW5'))if ($code != ('SW1W' || true))if ($code != true)If you're checking multiple values, you need to specificy the comparison each time. You don't just list out values separated by a boolean operator, a boolean operator always returns true or false. It may make sense for you to read it the way you think it works, but that's not the way the computer reads it.It will probably be more efficient and easier to read to put all of the values to test in an array and then use in_array.Also, PHP doesn't care about whitespace. as long as tokens are separated.

Link to comment
Share on other sites

That if statement isn't correct, guys. Look at the order of operations. Look at what this reduces down to:if ($code!= ('SW1W' || 'SW3' || 'SW5'))if ($code != ('SW1W' || true))if ($code != true)If you're checking multiple values, you need to specificy the comparison each time. You don't just list out values separated by a boolean operator, a boolean operator always returns true or false. It may make sense for you to read it the way you think it works, but that's not the way the computer reads it.It will probably be more efficient and easier to read to put all of the values to test in an array and then use in_array.Also, PHP doesn't care about whitespace. as long as tokens are separated.
How would I use an in_array? I've never used it before
Link to comment
Share on other sites

Thanks that was helpful and I think that it's nearly there, but it's still not checking the array at the moment. The code is now:
<?php$a = array("SW1W", "SW3", "SW5", "SW6", "SW10", "SW11", "SW15", "SW18", "W6", "W14", "W6", "W14");$code = $_GET['code'];$code2 = substr($_GET['code2'],0,1); // gets the first character.if (in_array($code,$a, TRUE))  {  echo "Match found<br />";  }else  {  echo "Match not found<br />";  } ?>

I want to check the results from the form against the array, but every time it just says "Match not found" even if I put the right thing in the 'code' field on the previous page.

Link to comment
Share on other sites

Have you confirmed the value of $code is what you're expecting? Try calling var_dump() with it.
Okay I've just discovered that the array is case sensitive! I didn't realizes that before - sorry. Can I make all of the text from the form upper-case regardless of whether somebody enters is as upper or lower case?
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...