search form with multiple inputs, how do i ignore the empty fields?

Discussion in 'PHP' started by SedNaX, Feb 25, 2007.

  1. #1
    I'm creating this search script, and the search form has a few fields (like name, address and date of birth).

    Only one (or more) can be entered, how do I ignore the fields that are not filled in ($_REQUEST['name'] = "") in the mysql query??

    I've been trying things for hours now, and just don't know it:eek:
     
    SedNaX, Feb 25, 2007 IP
  2. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if (!empty($bla)) {
       // do stuff
    }
    PHP:
     
    Icheb, Feb 25, 2007 IP
  3. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #3
    yes i know that.. but here's my problem. I have these fields

    first name
    last name
    address
    age

    someone enters age and first name, and someone else enters last name and address.

    I need to alter the mysql query that the empty strings are ignored..

    if i do it your way, i will have to do it this way:

    if (!empty($name)) {
    mysql query for last name, address and age
    }
    if (!empty($lastname)) {
    mysql query for name, address and age
    }
    if (!empty($age)) {
    mysql query for name, last name, address
    }
    if (!empty($address)) {
    mysql query for name, last name and age
    }

    that's only for when there's only one blank field. then i need all combinations for two blank fields, three blank fields, etc (the real form has around 8 fields, so that would be thousands of queries..)
     
    SedNaX, Feb 25, 2007 IP
  4. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You just make it modular.

    Something like this

    $query = '';
    if (!empty($name)) {
       $query .= ' AND name = \'blablabla\'';
    }
    if (!empty($address)) {
       $query .= ' AND address = \'blablabla\'';
    }
    
    mysql_query('SELECT blablabla FROM blabla WHERE 1 = 1 AND '. $query);
    PHP:
     
    Icheb, Feb 25, 2007 IP
    SedNaX likes this.
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Is it a search query that you're doing? I'm assuming so...

    You could always do something like...

    
    $query_array = array();
    if ( ! empty( $name ) ) {
        $query_array[] = 'name LIKE ' . $name;
    }
    if ( ! empty( $lastname ) ) {
        $query_array[] = 'lastname LIKE ' . $lastname;
    }
    
    ... etc ...
    
    $query_where = implode( ' AND ', $query_array );
    $query = 'SELECT FROM blah WHERE ' . $query_where;
    
    PHP:
    Hopefully that makes sense...
     
    TwistMyArm, Feb 25, 2007 IP
    SedNaX likes this.
  6. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    My approach is so much easier. :p
     
    Icheb, Feb 25, 2007 IP
  7. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Icheb: I would probably have to agree with you... we were obviously typing at the same time as there is no way I would type my own reply after seeing yours.

    Oh well, that's life I guess!
     
    TwistMyArm, Feb 25, 2007 IP
  8. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    And here I thought you would have been able to read the thread and post a reply within 2 minutes or less. I guess I was wrong.

    Oh well, that's life I guess!

    :p
     
    Icheb, Feb 25, 2007 IP
  9. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #9
    yes when i woke up this morning i thought of exact the same solution icheb ;)

    Thank you both a lot :)
     
    SedNaX, Feb 26, 2007 IP
  10. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #10
    okay i have another problem.. how do i change

    $query .= ' AND address = \'blablabla\'';

    so that blablabla becomes a string...?? I have to remove the quotes, otherwise the output becomes $name, but when i do that, the output becomes

    john doe

    instead of

    'john doe'

    EDIT: never mind, i have changed it... i had to change the string itself into "$name" :)
     
    SedNaX, Feb 26, 2007 IP
  11. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #11
    @Icheb: I can't quite tell from your post, but I hope you are being sarcastic... when I open all unread threads in separate tabs before even reading the first one, there are times where I don't even get to reading the thread until 15 minutes after opening it!
     
    TwistMyArm, Feb 26, 2007 IP