View Full Version : advanced search

01-19-2012, 02:57 PM
How can I ignore a field or a value if that is 0 or ' ' ?

Using 4 filters.

field1: value="0", value="1", value="2", value="3";
field2: value="0", value="1", value="2", value="3";
field3: value="0", value="1", value="2", value="3";
field4: value="0", value="1", value="2", value="3";

if $field = "0" ignore the field

why dosn't work this way?

if( $field1 != "0"){
$string .= "field1='".$_GET['field1']."' AND";

SELECT * FROM locatii $string



01-19-2012, 03:27 PM
Well, what is $field1? That comparison is perfectly fine, but without knowing what $field1 is there is no way to tell you what the problem is.
I'm also assuming that you are escaping this $_GET['field1'] somewhere to prevent SQL injections.

01-19-2012, 04:31 PM
$field1 = $_GET['field1'];

$field1 is the value, the word selected from the first dropdown field.

please see this post http://www.codingforums.com/showthread.php?p=1182290

01-19-2012, 04:37 PM
$field1 = $_GET['field1'];

please see this post http://www.codingforums.com/showthread.php?p=1182290

I don't care what it refers to, I need to know the value of it:


What is the result of this?

01-19-2012, 05:17 PM
when I search using filed 2 the results was

string(5) "blank" string(7) "Vrancea" string(5) "blank" string(5) "blank"

"blank" is 0

so what`s with that?

01-19-2012, 05:27 PM
Blank is not the same as 0. Given this, its value is the literal string "blank", and this is what you have to compare it to.

$aWhere = array();
if (strcasecmp($field1, "blank") != 0)
$aWhere['field1'] = $field1;
$sWhere = '';
if (!empty($aWhere))
$sWhere = ' WHERE ';
$i = 0;
foreach ($aWhere AS $field => $value)
if ($i++ > 0)
$sWhere .= ' AND ';
$sWhere .= sprintf("%s='%s'", $field, mysql_real_escape_string($value));

$sQry = 'SELECT * FROM table' . $sWhere;

I used 'field1' as the name of the field, since I don't know what it would be called otherwise.

01-19-2012, 05:46 PM
I have 4 dropdown menu.
every dropdown menu has the first row >SELECT< and the value of this row now is "blank", I tryied with "0" and with "" but same results. If I don't set any value, it will apear and search for ">SELECT<".

I wan't when the dropmenu is on >SELECT< to ignore it, and search for the other words from the other dropdonw menus that are not on ">SELECT<".

I really don't know how else to explain it...

I just need a source for advanced search
such as used on dating site or ecommerce shops.

search in database using words selected from the dropdown menus or check boxes....

I need it simple and safe....

and this si part of what I have now...
it works... but it dosn't :)
when I select 1 dropdown... it's ok
when I select 2 dropdown... it's ok...
but when I select a 3rd dropdown ... gives 0 results...

and if echo the query... it looks good... but... I can't see where is the problem...

$string="WHERE ";
$string .= "field1='".$_GET['field1']."' AND ";
$string .= "field2='".$_GET['field2']."' AND ";
$string .= "field3='".$_GET['field3']."' AND ";
$string .= "field4='".$_GET['field4']."' AND ";
$string = substr($string,0,-4);
$string .="ORDER BY id DESC";

$query = "SELECT * FROM table $string";

01-19-2012, 06:46 PM
Did you look at my code above? The results of your unchanged select options will be the string "blank". You need to compare to this string to determine if its been changed, and only deal with it if it has been.
If you specify 0 as your option value, an implicit cast in a comparison to a boolean will occur due to PHP's datatype weakness. So you can use it in a comparison for the if, although using an if (!empty($array['field'])) is a better option since empty will squelch notices on undefined offsets.
Selecting more filter criteria without a result doesn't necessarily indicate a problem. It means there is no match to the query you have provided for a result. So selecting a third on top of the two prior would simply say there is no results matching where all three conditions are true.