Jump to content

two variables in elseif statement


benji87

Recommended Posts

Hi all im trying to setup a search filter in which i have two input fields. But what time trying to do is set up a statement if both the fields are set to all. So basicly display everything in the database.

 

This is the code i have at the moment but it doesnt return anything

elseif($area == "Portsmouth" && $act_type == "All")
{
     $query=("SELECT * FROM events ORDER BY date DESC LIMIT 0,10");
  }

 

Can anybody help me out please?

 

Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/80287-two-variables-in-elseif-statement/
Share on other sites

I am not 100 % sure what your trying to do, but this is what I think,

 

<?php

//read in your search filter fields

if ($field1=="all" && $field2=="all") {
$sql = "SELECT * FROM mytable";
} else {

//All your other possible conditions here for example
$sql = "SELECT * FROM mytable WHERE field1 = $field1 AND field2 = $field2";


}

?>

this is my full code

 

if($act_type == "All") 
  {
     $query=("SELECT * FROM events WHERE area = '$area' ORDER BY date DESC LIMIT 0,10");
  }
elseif($area == "Portsmouth")
  {
     $query=("SELECT * FROM events WHERE act_type = '$act_type' ORDER BY date DESC LIMIT 0,10");
  }
elseif($area == "Portsmouth" && $act_type == "All")
  {
     $query=("SELECT * FROM events ORDER BY date DESC LIMIT 0,10");
  }
else
  {
     $query=("SELECT * FROM events WHERE act_type = '$act_type' AND area = '$area' ORDER BY date DESC LIMIT 0,10");
  }
$result=mysql_query($query);
$num=mysql_numrows($result);


$i=0;
while ($i < $num) {

$event=mysql_result($result,$i,"event");
$date=mysql_result($result,$i,"date");
$time=mysql_result($result,$i,"time");
$act_type=mysql_result($result,$i,"act_type");
$id=mysql_result($result,$i,"id");
$colour=mysql_result($result,$i,"colour");
?>

 

All the other elseif statements work apart from the one where i want to return all the results.

 

Thanks

You should really do the check on both fields before the checks on singular fields as in the above example, a value for portsmouth and all will not be evaluated because portsmouth evaluates already prior to your code reaching that final elseif.

Why are you using mysql_result when you want to loop through all the rows? Do this

 

<?php

$result=mysql_query($query)or die(mysql_error());
$num=mysql_numrows($result);

while ($row = mysql_fetch_assoc($result)){
   echo $row['event'].'<br>';
}

?>

 

I also put a die statement at the end of your query.

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.