SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need help with processing a search query

    I'm not very experienced with PHP but I need to modify a dating script, specifically a quick search box where a user can select "Male", "Female" or "Both" from the "Looking For" drop down box and get the relevant search results. So if Both was selected, you'd get listings of both male and female.

    The form sends data to a php page that processes the search query.
    Here's part of the current code:

    if($search_looking_for != "Female")
    {
    $sql_where = "p.gender='Male' ";
    }
    else
    {
    $sql_where = "p.gender='Female' ";
    }

    This works fine, but how would I add the "Both" option? I tried adding elseif statement with:

    $sql_where = "p.gender='Male' and p.gender='Female' ";

    but this doesn't return the correct results.

    Any ideas on how to do this would be greatly appreciated!

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    switch and if - elseif - else example

    PHP Code:
    <?php
    switch ($search_looking_for) {
        case 
    'Female':
            
    $sql_where "p.gender='Female' ";
            break;

        case 
    'Male':
            
    $sql_where "p.gender='Male' ";
            break;

        default:
            
    $sql_where "p.gender='Male' and p.gender='Female' ";
    }



    if(
    $search_looking_for == "Male") {
        
    $sql_where "p.gender='Male' ";

    } elseif( 
    $search_looking_for == "Female") {
        
    $sql_where "p.gender='Female' ";

    } else {
        
    $sql_where "p.gender='Male' and p.gender='Female' ";

    }

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by flash2

    if($search_looking_for == "Male") {
    $sql_where = "p.gender='Male' ";

    } elseif( $search_looking_for == "Female") {
    $sql_where = "p.gender='Female' ";

    } else {
    $sql_where = "p.gender='Male' and p.gender='Female' ";

    }

    Just tried the elseif example, it works when looking for either male or female, but when Both is selected, it doesn't return any results. The switch/case doesn't work at all regardless of what is selected. Hmmmmm...

  4. #4
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I won't comment on the moral or humorous aspects of what your code says, but try:
    PHP Code:
    switch($search_looking_for) {
    case 
    'Male':
        
    $sql_where "p.gender='Male' ";
        break;
    case 
    'Female':
        
    $sql_where "p.gender='Female' ";
        break;
    default:
        
    $sql_where "1";

    Christopher


Bookmarks

Posting Permissions

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