Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: advanced search

  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts

    advanced search

    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


    ?????

    thx

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts
    $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
    Last edited by _user; 01-19-2012 at 04:38 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by _user View Post
    $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:
    PHP Code:
    var_dump($field1); 
    What is the result of this?

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?
    Last edited by _user; 01-19-2012 at 05:26 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    $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'"$fieldmysql_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.

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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...

    PHP Code:
    $string="WHERE ";
    $flag=0;
    if(
    $_GET['field1']){
    $string .= "field1='".$_GET['field1']."' AND ";
    $flag=1;
    }
    if(
    $_GET['field2']){
    $string .= "field2='".$_GET['field2']."' AND ";
    $flag=1;
    }
    if(
    $_GET['field3']){
    $string .= "field3='".$_GET['field3']."' AND ";
    $flag=1;
    }
    if(
    $_GET['field4']){
    $string .= "field4='".$_GET['field4']."' AND ";
    $flag=1;
    }
    if(
    $flag==1){
    $string substr($string,0,-4);
    $string .="ORDER BY id DESC";
    }

    $query "SELECT * FROM table $string"
    Last edited by _user; 01-19-2012 at 05:53 PM.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •