Jump to content

Multiple if statements


sp@rky13

Recommended Posts

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)

Link to comment
https://forums.phpfreaks.com/topic/181077-multiple-if-statements/
Share on other sites

@ 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";

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";
}

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

@ 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);?>

@ 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??

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.