mikemcg36 Posted March 19, 2012 Share Posted March 19, 2012 Hello, I need help filtering an SQL query based on the combination of drop down menus. I have tried using this code found in another thread but I am still getting all rows selected. Any ideas?? Thank you. Here is my html <form name="xml.php" method="POST"> <input type="button" id="showmarkers" value="Show Markers" /> <select name="meetingType"> <option value="All Types" selected="All Types">All Types</option> <option value="fun">fun</option> <option value="work">work</option> </select> <select name="meetingDay"> <option value="All Days" selected="All Days">All Days</option> <option value="Monday">Monday</option> <option value="Tuesday">Tuesday</option> <option value="Wednesday">Wednesday</option> <option value="Thursday">Thursday</option> <option value="Friday">Friday</option> <option value="Saturday">Saturday</option> <option value="Sunday">Sunday</option> </select> <select name="meetingTime"> <option value="All Times" selected="All Times">All Times</option> <option value="Early">Early</option> <option value="Noon">Noon</option> <option value="Late">Late</option> </select> </form> And the PHP: $whereClauses = array(); if (! empty($_POST['meetingType'])) $whereClauses[] ="meetingType='".mysql_real_escape_string($_POST['meetingType'])."'"; if (! empty($_POST['meetingDay'])) $whereClauses[] ="meetingDay='".mysql_real_escape_string($_POST['meetingDay'])."'"; if (! empty($_POST['meetingTime'])) $whereClauses[] ="meetingTime='".mysql_real_escape_string($_POST['meetingTime'])."'"; $where = ''; if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } $resultID = mysql_query("SELECT * FROM meetings".$where); Quote Link to comment https://forums.phpfreaks.com/topic/259290-help-with-filtering-a-query-via-drop-downs/ Share on other sites More sharing options...
Mahngiel Posted March 19, 2012 Share Posted March 19, 2012 I have used this with success. jQuery Chained. Here is an example using it: <?php if($cats):?> <select id="cat" name="category_id"> <option value="">Game Genre</option> <?php foreach($cats as $cat):?> <option value="<?php echo $cat->category_id;?>"><?php echo $cat->category_title; ?></option> <?php endforeach; ?> </select> <?php endif; ?> <?php if($subs):?> <select id="sub" name="sub_cat_id"> <option value="">Sub Genre</option> <?php foreach($subs as $sub):?> <option value="<?php echo $sub->sub_cat_id;?>" class="<?php echo $sub->setting_id;?>"><?php echo $sub->sub_cat_name; ?></option> <?php endforeach; ?> </select> <?php endif; ?> <script type="text/javascript" > $("#sub").chained("#cat"); </script> Quote Link to comment https://forums.phpfreaks.com/topic/259290-help-with-filtering-a-query-via-drop-downs/#findComment-1329186 Share on other sites More sharing options...
mikemcg36 Posted March 20, 2012 Author Share Posted March 20, 2012 Not sure I completely understand this jquery method. Is there no way to do it how I am doing it? Quote Link to comment https://forums.phpfreaks.com/topic/259290-help-with-filtering-a-query-via-drop-downs/#findComment-1329400 Share on other sites More sharing options...
Mahngiel Posted March 20, 2012 Share Posted March 20, 2012 My apologies, when I read "combination drop down", I stopped reading and assumed you meant something else. What is your DB table structure like and can you provide some output of $whereClauses? Quote Link to comment https://forums.phpfreaks.com/topic/259290-help-with-filtering-a-query-via-drop-downs/#findComment-1329414 Share on other sites More sharing options...
seanlim Posted March 20, 2012 Share Posted March 20, 2012 Maybe you should var_dump() the SQL query, just to make sure that the POST variables are getting passed properly, you SQL syntax is well-formed, and that the SQL database is returning the desired results. Quote Link to comment https://forums.phpfreaks.com/topic/259290-help-with-filtering-a-query-via-drop-downs/#findComment-1329415 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.