sp@rky13 Posted November 11, 2009 Share Posted November 11, 2009 Ok so seems basic but for some reason I'm not doing it right. What I want is to be able to have 2 separate if statements in the same php document. What I've done is this: if ($p1=='on' AND $p2=='') $include = " AND points >= '$pointsmin'"; else if ($p1=='' AND $p2=='on') $include = " AND points <= '$pointsmax'"; else if ($p1=='on' AND $p2=='on') $include = " AND points BETWEEN '$pointsmin' AND '$pointsmax'"; else if ($p1=='' AND $p2=='') $include = ""; */Second if statement/* if {$k =='on'} $betweenclause = "x BETWEEN $xLow AND $xHigh AND y BETWEEN $yLow AND $yHigh"; else if {$x=='on' AND $y==''} $betweenclause = "x BETWEEN $x1 AND $x2"; (The comment in there is not actually in my code so don't worry about it) Quote Link to comment https://forums.phpfreaks.com/topic/181077-multiple-if-statements/ Share on other sites More sharing options...
exally Posted November 11, 2009 Share Posted November 11, 2009 Look at this, i can understand the want to speed up the code by losing the '{'. But in this case put that back in. The second if and else if statements need ( not { styled brackets. Quote Link to comment https://forums.phpfreaks.com/topic/181077-multiple-if-statements/#findComment-955420 Share on other sites More sharing options...
Gayner Posted November 11, 2009 Share Posted November 11, 2009 You can have as many if's in your code as the pubic hair on mine! Quote Link to comment https://forums.phpfreaks.com/topic/181077-multiple-if-statements/#findComment-955421 Share on other sites More sharing options...
sp@rky13 Posted November 11, 2009 Author Share Posted November 11, 2009 @ gayner: A bit unnecessary @ exally: ty but it didm't help. Changed to this: if ($p1=='on' AND $p2=='') $include = " AND points >= '$pointsmin'"; else if ($p1=='' AND $p2=='on') $include = " AND points <= '$pointsmax'"; else if ($p1=='on' AND $p2=='on') $include = " AND points BETWEEN '$pointsmin' AND '$pointsmax'"; else if ($p1=='' AND $p2=='') $include = ""; if ($k =='on') $betweenclause = "x BETWEEN $xLow AND $xHigh AND y BETWEEN $yLow AND $yHigh"; else if ($x=='on' AND $y=='') $betweenclause = "x BETWEEN $x1 AND $x2"; Quote Link to comment https://forums.phpfreaks.com/topic/181077-multiple-if-statements/#findComment-955424 Share on other sites More sharing options...
iversonm Posted November 11, 2009 Share Posted November 11, 2009 You need to always include the conditions in () and then the result {}, You can always add as many if statements as you want in any script. Here is the way your script should be written if ($p1=='on' && $p2==''){ $include = " AND points >= '$pointsmin'"; }else if ($p1=='' AND $p2=='on'){ $include = " AND points <= '$pointsmax'"; }else if ($p1=='on' AND $p2=='on'){ $include = " AND points BETWEEN '$pointsmin' AND '$pointsmax'"; }else if ($p1=='' AND $p2==''){ $include = ""; } if ($k =='on'){ $betweenclause = "x BETWEEN $xLow AND $xHigh AND y BETWEEN $yLow AND $yHigh"; }else if ($x=='on' AND $y==''){ $betweenclause = "x BETWEEN $x1 AND $x2"; } Quote Link to comment https://forums.phpfreaks.com/topic/181077-multiple-if-statements/#findComment-955425 Share on other sites More sharing options...
exally Posted November 11, 2009 Share Posted November 11, 2009 iversonm - i test the following code. If you leave out the {} brackets it will execute or not to the next ';' (semi-colon). <?php $p1 = ""; $p2 = 'on'; $k = 'on'; $pointsmin = 'ptmin'; $pointsmax = 'ptmax'; $xLow = 1; $xHigh = 3; $yLow = 0; $yHigh = 4; $x1 = 2; $x2 = 3; if ($p1=='on' AND $p2=='') $include = " AND points >= '$pointsmin'"; else if ($p1=='' AND $p2=='on') $include = " AND points <= '$pointsmax'"; else if ($p1=='on' AND $p2=='on') $include = " AND points BETWEEN '$pointsmin' AND '$pointsmax'"; else if ($p1=='' AND $p2=='') $include = ""; if ($k =='on') $betweenclause = "x BETWEEN $xLow AND $xHigh AND y BETWEEN $yLow AND $yHigh"; else if ($x=='on' AND $y=='') $betweenclause = "x BETWEEN $x1 AND $x2"; print $include . "<br/>"; print $betweenclause . "<br/>"; ?> and got the result of AND points <= 'ptmax' x BETWEEN 1 AND 3 AND y BETWEEN 0 AND 4 Quote Link to comment https://forums.phpfreaks.com/topic/181077-multiple-if-statements/#findComment-955429 Share on other sites More sharing options...
sp@rky13 Posted November 11, 2009 Author Share Posted November 11, 2009 @ iversonm ty again but still errors. Here's the whole code to be safe: <?php $yLow = floor($k1 / 10) * 100; $yHigh = $yLow + 99; $xLow = ($k1 % 10) * 100; $xHigh = $xLow + 99; if ($p1=='on' AND $p2=='') {$include = " AND points >= '$pointsmin'";} else if ($p1=='' AND $p2=='on') {$include = " AND points <= '$pointsmax'";} else if ($p1=='on' AND $p2=='on') {$include = " AND points BETWEEN '$pointsmin' AND '$pointsmax'";} else if ($p1=='' AND $p2=='') {$include = "";} if ($k =='on') {$betweenclause = "x BETWEEN $xLow AND $xHigh AND y BETWEEN $yLow AND $yHigh";} else if ($x=='on' AND $y=='') {$betweenclause = "x BETWEEN $x1 AND $x2";} $con = mysql_connect("*****","*************","********"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("********", $con); $result = mysql_query("SELECT * FROM village$world WHERE player = '0' AND $betweenclause $include"); while($row = mysql_fetch_array($result)) { echo "[village]".$row['x']."|".$row['y']."[/village]"."\n"; } mysql_close($con);?> Quote Link to comment https://forums.phpfreaks.com/topic/181077-multiple-if-statements/#findComment-955430 Share on other sites More sharing options...
exally Posted November 11, 2009 Share Posted November 11, 2009 @ iversonm ty again but still errors. Here's the whole code to be safe: <?php $yLow = floor($k1 / 10) * 100; $yHigh = $yLow + 99; $xLow = ($k1 % 10) * 100; $xHigh = $xLow + 99; if ($p1=='on' AND $p2=='') {$include = " AND points >= '$pointsmin'";} else if ($p1=='' AND $p2=='on') {$include = " AND points <= '$pointsmax'";} else if ($p1=='on' AND $p2=='on') {$include = " AND points BETWEEN '$pointsmin' AND '$pointsmax'";} else if ($p1=='' AND $p2=='') {$include = "";} if ($k =='on') {$betweenclause = "x BETWEEN $xLow AND $xHigh AND y BETWEEN $yLow AND $yHigh";} else if ($x=='on' AND $y=='') {$betweenclause = "x BETWEEN $x1 AND $x2";} $con = mysql_connect("*****","*************","********"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("********", $con); $result = mysql_query("SELECT * FROM village$world WHERE player = '0' AND $betweenclause $include"); while($row = mysql_fetch_array($result)) { echo "[village]".$row['x']."|".$row['y']."[/village]"."\n"; } mysql_close($con);?> your Parameters for the If statements aren't being set? if you are receiving them over post or get declare them via $var = $_POST['var']; maybe?? Quote Link to comment https://forums.phpfreaks.com/topic/181077-multiple-if-statements/#findComment-955432 Share on other sites More sharing options...
sp@rky13 Posted November 11, 2009 Author Share Posted November 11, 2009 I'm getting them via get. Everything worked fine until I added in the second set of if statements Quote Link to comment https://forums.phpfreaks.com/topic/181077-multiple-if-statements/#findComment-955433 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.