Results 1 to 8 of 8

Thread: create multiple drop down menus to filter mysql query?

  1. #1
    Join Date
    Jun 2009

    create multiple drop down menus to filter mysql query?

    Hello all,
    I've been using webdeveloper.com for a while now but I never needed to ask my own question since I usually found it in search, but this time its different

    Here is my problem,

    I am currently building a page that uses five separate drop down menus (static content) that when selected the combination of what is selected will filter my mysql query and display the correct results using those parameters.

    I have everything set up to work when one or another variable is sent, however I am having trouble creating a way to set up the query string where non of the filter options are required.

    here is the code that I have so far.

    This is my menu code, this will always be static.
    HTML Code:
    <form name="campsite_search" action="page.html" method="get">
        <select name="state" class="input">
            <option value="all">Select a State</option>
            <option value="MD">Maryland</option>
            <option value="VA">Virginia</option>
            <option value="WV">West Virginia</option>                                                    
            <option value="PA">Pennsylvania</option>
            <option value="DC">Washington D.C.</option>
        <select name="radius" class="input">
        	<option value="all">Radius from Council</option>
            <option value="25">25 miles</option>
            <option value="50">50 miles</option>
            <option value="100">100 miles</option>
            <option value="250">250 miles</option>
            <option value="500">500 miles</option>    
        <select name="size" class="input">
            <option value="all">Please select Camp Size</option>
            <option value="25">25 Campers</option>
            <option value="50">50 Campers</option>
            <option value="100">100 Campers</option>
            <option value="250">250 Campers</option>
            <option value="500">500 Campers</option>
        <select name="type" class="input">
    	    <option value="all">Structure type</option>
    	    <option value="tent">Tents</option>
    	    <option value="cabin">Cabins</option>    
        <select name="activity" class="input">
        	<option value="all">High Adventure Activities</option>
            <option value="0">No</option>
            <option value="1">Yes</option>
    <input type="submit" name="submit" class="input" value="Continue" />
    and here is my php code that deals with the query, it is far from finished, not even using all the options in the drop down. I started trying to build a big if/elseif tree but that isn't working out. I am also using the smarty and pear libraries, so I apologize if it looks non-standard.

    PHP Code:
                  if(isset($_GET['state'])){$state addslashes($_GET['state']);}
    $radius addslashes($_GET['radius']);
    $size addslashes($_GET['size']);
    //dealing with the single filter options
    if(($state 'all')&&($radius 'all')&&($size 'all')){
    $filter ='';
    $state != 'all')&&($radius 'all')&&($size 'all')){                  
    $filter "state ='".$state."' ";
    $state 'all')&&($radius != 'all')&&($size 'all')){                  
    $filter "radius <='".$radius."' ";              
    $state 'all')&&($radius 'all')&&($size != 'all')){
    $filter "camp_size <='".$size."' ";
    $order "camp_size ASC";

    $rs $a->select($view,$filter,$order,$offset,$limit); 
    what I need help with is a way to craft the filtering string so that the options are not required, a catch all way to select any combination of filters and for the query to work.

    Any help would be appreciated, and Thank you very much for your time.

  2. #2
    Join Date
    Jan 2005
    Alicante (Spain)
    I don't really know what you are trying to do, the question isn't too clear. Maybe something like this:
    PHP Code:
    $filter ='';
    $state !== 'all')
    $filter .= ", state ='".$state."' ";

    $radius !== 'all')
    $filter .= ", radius ='".$radius."' ";

    $size !== 'all')
    $filter .= ", camp_size ='".$size."' ";

    Quote Originally Posted by nickpalmigiano View Post
    PHP Code:
    elseif(($state 'all'
    That will always be true. You are setting a variable, not checking for equality.

  3. #3
    Join Date
    Jun 2009

    This thread and post number four explain it a little better than I did. I am trying to write the tree so that any combination of selections in the drop down will result in a successful query. ie. state only or state, size and activity, or radius and activity.

    is that any better?

    Quote Originally Posted by bokeh View Post
    I don't really know what you are trying to do, the question isn't too clear. Maybe something like this:
    PHP Code:
    $filter ='';
    $state !== 'all')
    $filter .= ", state ='".$state."' ";

    $radius !== 'all')
    $filter .= ", radius ='".$radius."' ";

    $size !== 'all')
    $filter .= ", camp_size ='".$size."' ";

    That will always be true. You are setting a variable, not checking for equality.
    AHH! I feel silly, thank you! that did fix the tree for all the single drop down selections. Like, if someone only picks state or radius and leaves the rest alone. But what about the multiple selections?

  4. #4
    Join Date
    Jan 2005
    Alicante (Spain)

  5. #5
    Join Date
    Jun 2009
    PHP Code:
                $state $db->escapeSimple($_GET['state']);
    $radius $db->escapeSimple($_GET['radius']);
    $size $db->escapeSimple($_GET['size']);
    $type $db->escapeSimple($_GET['type']);
    $activity $db->escapeSimple($_GET['activity']);
    $filter "";
    $state !== 'all'){                  
    $filter .= "state ='".$state."'";
    $order "state ASC";
    $radius !== 'all'){                  
    $state)&&($state !== 'all')){
    $filter .= " AND ";
    $filter .= "radius <='".$radius."'";
    $order "radius ASC";
    $size !== 'all'){
    $radius)&&($radius !== 'all')||isset($state)&&($state !== 'all')){
    $filter .= " AND ";
    $filter .= "camp_size <='".$size."'";
    $order "camp_size ASC";
    $type !== 'all'){
    $radius)&&($radius !== 'all')||isset($state)&&($state !== 'all')||isset($size)&&($size !== 'all')){
    $filter .= " AND ";
    $filter .= "type LIKE '%".$type."%'";
    $order "type ASC";
    $activity !== 'all'){
    $radius)&&($radius !== 'all')||isset($state)&&($state !== 'all')||isset($size)&&($size !== 'all')||isset($type)&&($type !== 'all')){
    $filter .= " AND ";
    $filter .= "canoe ='".$activity."'";
    $order "lastmod ASC";

    $rs $a->select($view,$filter,$order,$offset,$limit); 
    I was able to get it all to work with some tweaking and the direction you pointed me in. Thank you very much for all your help. Might be a little overkill but it works haha just have to work on optimizing my code.

  6. #6
    Join Date
    Apr 2010

    query help

    Hello, sorry to reply for and old thread. But I been google around and this is the same thing that i needed help with. I have 2 drop down menu and a submit button. I try to us nickpalmigiano's if condition to filter result, but where do I apply the $filter and $order variables on the Mysql_query. Does this code look right...

    $sql = mysql_query("SELECT * FROM mytable WHERE Category='".$filter."' && SubCat='".$filter."' ORDER BY $order");

    --below is my 2 drop down menu populated using javascript--
    <FORM name="drop_list" action="joboutput2.php" method="POST" >
    <SELECT NAME="Category" size="1" id="Category" onChange="SelectSubCat();" >
    <Option value="All">所有</option>
    <SELECT NAME="SubCat" id="SubCat" >
    <Option value="All">所有</option>
    <input type="Submit" name="Form3" value="搜索"/>
    Last edited by ychan623; 03-10-2012 at 01:36 AM.

  7. #7
    Join Date
    Feb 2012
    you can't use same variable for both category and sub-category as i think, both will be having different values..in this case, you just need to use following code:
    $sql = mysql_query("SELECT * FROM mytable WHERE Category='$category' and SubCat='$subcategory' ");

  8. #8
    Join Date
    Oct 2012
    hi there. I created HTML and PHP file to filter web form data based on multiple drop down filters. Here goes the form. when i run the form and PHP, i don't see any results in the browser. There is no error either. I am working on a example posted by another forum member.Pls. help. Thanks in advance.

    <form action="showJobs_new.php" method="post">
    <select name="Locations">
    <option value="" selected="selected">All Locations</option>
    <option value="arizona">Arizona</option>
    <option value="alaska">Alaska</option>
    <select name="Jobs">
    <option value="" selected="selected">All jobs</option>
    <option value="Carpenter">Carpenters</option>
    <option value="Plumbers">Plumbers</option>
    <input type="submit" value="search jobs" />





    @mysql_select_db($database) or die( "Unable to select database");

    $whereClauses = array();
    if (! empty($_POST['Locations'])) $whereClauses[] ="Locations='".mysql_real_escape_string($_POST['Locations'])."'";
    if (! empty($_POST['Jobs'])) $whereClauses[] ="Jobs='".mysql_real_escape_string($_POST['Jobs'])."'";
    $where = '';
    if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); }

    $sql = mysql_query("SELECT * FROM mytable ORDER BY id DESC $limit" .$where);


    while ($row = mysql_fetch_assoc($result)) {
    echo $row['Locations'];
    echo $row['Jobs'];


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.13926 seconds
  • Memory Usage 3,002KB
  • Queries Executed 13 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_html
  • (5)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates