SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot cherie's Avatar
    Join Date
    Nov 2004
    Location
    Roxbury, NY
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    advanced search with pulldown menus questions

    I have a page that has 4 pulldowns on it: artist, genre, series, label.

    Is it possible for me to allow a user to make a choice from each of these or a combination of these to return search results?

    If so (because it has to be, this is PHP and mySql), can you point me towards a how to/tutorial?
    Cherie
    foamyindustries.com
    Movies We Watch - we watch it and tell you what we think
    chickenflicker.com - everyone needs a chicken a day

  2. #2
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is some code to get you started.

    It is not complete - I have left lots for you to do, but it should give you a start in the right direction.

    PHP Code:
     
     
     
    /*
     These arrays will probably be bult from your database tables like
     
     $sql = 'SELECT artist_name FROM artist_table ORDER BY artist_name ASC';
     $result = mysql_query($sql) or die ($sql . ' failed due to ' . mysql_error());
     while ($row = mysql_fetch_assoc($result))
     {
         $artist_names[] = $row['artist_name'];
     }
     
     Similarly for yout other drop-down list boxes
     
     */
     
     
    $artist_names[] = 'Artist 1';
     
    $artist_names[] = 'Artist 2';
     
    $artist_names[] = 'Artist 3';
     
     
    $genre_names[] = 'Genre 1';
     
    $genre_names[] = 'Genre 2';
     
    $genre_names[] = 'Genre 3';
     
     
     if (
    true === isset($_POST['Search']))
     {
         
    $where_clause '';
         
    // handle search
         
    if (true === isset($_POST['artist_name']))
         {
             
    // Check for search by artist
             
    $a_name $_POST['artist_name'];
             if (
    strlen($a_name))
             {
               
    $where[] = ' artist_name = \'' $a_name '\'';
             }
     
             
    // check for search by genre
             
    $g_name $_POST['genre_name'];
             if (
    strlen($g_name))
             {
               
    $where[] = ' genre_name = \'' $g_name '\'';
             }
     
             if (
    true === is_array($where))
             {
                 
    $where_clause ' WHERE ' implode(' AND '$where);
             }
         }
     
         
    /*
             build your query to search the appropriate talbe(s)
             and add the $where_clasuse to it, something like
             $sql=     'SELECT
                                     artist_name
                                 , genre_name
                             FROM
                                     artist_table
                                 , genre_table
                             INNER JOIN
                                     artist_table.genre_id = genre_table.genre_id
                             ' . $where_clause;
         */
     
     
    echo 'Searcing Database using ' $where_clause;
     }
     
     
     
    // Start Form
     
    $html_form '<form action="' $_SERVER[PHP_SELF] . '" method="POST">';
     
     
    // Artist drop-down
     
    $html_form .= 'Select Artist <br />';
     
    $html_form .= '<select name="artist_name">';
     
    $html_form .= '<option selected value="">All Artists</option>';
     foreach (
    $artist_names as $a_name)
     {
         
    $html_form .= '<option>' $a_name '</option>';
     }
     
    $html_form .= '</select"> <br /><br >';
     
     
    // Genre drop-down
     
    $html_form .= 'Select Genre <br />';
     
    $html_form .= '<select name="genre_name">';
     
    $html_form .= '<option selected value="">All Genres</option>';
     foreach (
    $genre_names as $g_name)
     {
         
    $html_form .= '<option>' $g_name '</option>';
     }
     
    $html_form .= '</select"> <br /> <br />';
     
     
    // Search button
     
    $html_form .= '<input type="submit" name="Search" value="Search" />';
     echo 
    $html_form

  3. #3
    SitePoint Zealot cherie's Avatar
    Join Date
    Nov 2004
    Location
    Roxbury, NY
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you. This is coming along slowly, but seems to be going somewhere.
    Cherie
    foamyindustries.com
    Movies We Watch - we watch it and tell you what we think
    chickenflicker.com - everyone needs a chicken a day

  4. #4
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My pleasure.
    Glad you're making progress. Keep at it


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
  •