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


}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.