Jump to content

Recommended Posts

Hi users!

 

I use this query to fetch results...

 

$q = "SELECT * FROM profile WHERE Country='$Country' AND Gender='$Gender' AND Age='$Age' ORDER BY Date DESC";

 

But users sometime select Any country, or Any age, etc..

 

How can i cancel  Country='$Country'  or Gender='$Gender' etc..  if a user chose Any

 

I can't use if function because the possibilities are endless

Oh ok. I misread your question the first time.

 

Try an OR statement wrapped in ()s or something.

 

$q = "SELECT * FROM profile WHERE (Country='$Country' OR Country='Any') AND Gender='$Gender' AND (Age='$Age' OR Age='Any') AND Country!='Any' AND Age!='Any' ORDER BY Date DESC";

 

 

 

Hello,

 

You need to check whether the user selects any country and creates SQL query according to that.

 

For Example:

 

if($_GET['country'] == "Any")

  $countrySql  = "";

else

$countrySql = " AND Country= '".$_GET['country']."' ";

 

 

Create sql query for all of the drop downs and finally make your sql statement as:

 

$q = "SELECT * FROM profile WHERE 1 $countrySql ";

 

 

Regards

Aniesh Joseph

anieshjoseph@gmail.com

Thanks charlieholder  for your reply

 

However, your above code will search for a Country called Any

 

I dont want that actually

 

I want when a users selects Any... The database simply shows all the countries

 

but if the user choses USA, then, database searches only for USA

So can i say...

 

if($_GET['country'] == "Any")

   $countrySql  = "";

else

$countrySql = " AND Country= '".$_GET['country']."' ";

 

if($_GET['age'] == "Any")

   $ageSql  = "";

else

$ageSql = " AND Age= '".$_GET['age']."' ";

 

$q = "SELECT * FROM profile WHERE 1 $countrySql AND $ageSql ";

 

is that ok?

and What is the 1 by the way?

 

Hello

 

A slight changes on your query . Please remove the "AND". Because we already added AND  in $countrySql and $ageSql.

 

$q = "SELECT * FROM profile WHERE 1 $countrySql  $ageSql ";

 

Suppose that at the first time you calling the page, do not have any "country or age " in the URL . So if you omit the 1 , the query will looks like this:

 

$q = "SELECT * FROM profile WHERE " ; /*  Nothing after the WHERE */

 

I think you got the idea now.

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.