benji87 Posted December 5, 2007 Share Posted December 5, 2007 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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 5, 2007 Share Posted December 5, 2007 Show us the code where you tell it to return something. Quote Link to comment Share on other sites More sharing options...
Cep Posted December 5, 2007 Share Posted December 5, 2007 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"; } ?> Quote Link to comment Share on other sites More sharing options...
benji87 Posted December 5, 2007 Author Share Posted December 5, 2007 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 Quote Link to comment Share on other sites More sharing options...
Cep Posted December 5, 2007 Share Posted December 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 5, 2007 Share Posted December 5, 2007 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. Quote Link to comment 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.