SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    France
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multi-option search

    I need to make a search page where one can do a search on sales statistics based on different options such as seller, region, period, type of product sold, company...

    So one might choose to search for just the sales of a certain seller, during a certain period, in just one region, or all the sales of one seller with one company and so on. Many different possible combinations.

    I need some guidance on how to approach this effectively. Should I just use different if statements for every possible combination?

  2. #2
    SitePoint Addict Adam A Flynn's Avatar
    Join Date
    Jul 2004
    Location
    Canada
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firstly, I assume the data sits in a MySQL database. If that's true, you can use the LIKE statement in a MySQL query, and wildcards to capture all the data you want in 1 query.

    I assume you have a series of dropdown menus for the various values for 'seller', 'region', etc. If they're textboxes, use something like:
    PHP Code:
    if(!isset($_POST['var'])) 

    $_POST['var'] = '%'

    Basically what you want is the value of $_POST['var'] to be '%' for anything that the user isn't searching by. (For dropdowns for any use <option value='%'>Any</option>)

    Then you want to include all of the things you can search by in the SQL query. For example:
    Code:
    SELECT * FROM sales WHERE Region LIKE '$_POST[region]' AND Seller LIKE '$_POST[seller]'
    In that example, if $_POST[region] = '%', then it will select where region is anything, and seller is whatever is said in the form. (In effect, allowing a user to search for any number of the search terms, but still using 1 query). The important part is to use 'LIKE' instead of '=' in the query.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    France
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent!

    Just what I wanted to know, thanks Adam.


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
  •