gorgon2k Posted July 29, 2010 Share Posted July 29, 2010 Ok, for school i'm trying to write a script that will give 4 dynamically generated drop down boxes that will allow you to sort through a vehicle database. Now i'm hung up on the part that will allow me to determine which ones are selected and throw them into a query. I was thinking if I wrote something like <?PHP $mak = $_GET['mak']; $mod = $_GET['mod']; $bst = $_GET['bst']; $year = $_GET['year']; $result = mysql_query("SELECT FROM automobile_stat WHERE body_style = $bst and make = $mak and model = $mod and year = $year" ) but what my issue is, what if one or some of the drop downs aren't selected, is there something I can put in as a default value for the variables that will return everything in that query? Say the user selects a Body style then selects a make, is there a way that all the models and years under those parameters would show using that query? Quote Link to comment Share on other sites More sharing options...
xcoderx Posted July 29, 2010 Share Posted July 29, 2010 <option value=''> </option> $errmsg_arr = array(); if($field == '') { $errmsg_arr[] = 'field is required'; $errflag = true; } something like this u mean? Quote Link to comment Share on other sites More sharing options...
gorgon2k Posted July 29, 2010 Author Share Posted July 29, 2010 well i dont want to make it required, I want to make it so theres 4 drop down menus across the top and each time you click one it regenerates the data. Quote Link to comment Share on other sites More sharing options...
AtlasC1 Posted July 29, 2010 Share Posted July 29, 2010 Hi Gorgon, To get the result you're looking for, you'll probably need to throw some javascript in there too. In order to reload the results, you'll need to add something like onClick="this.form.submit()" to your <select> fields. And to set a default, I would look into doing one-line if-else statements to keep your code clean and easy to read (lots of people hate these, but I think they work great when they're used in the right place): <?php $mak = (empty($_GET['mak'])) ? '*' : $_GET['mak']; $mod = (empty($_GET['mod'])) ? '*' : $_GET['mod']; // and so on... ?> This can be thought of as: if $_GET['mak'] is empty or not set, then $mak = '*'; otherwise, $mak = $_GET['mak']; Would something like that work? -jm p.s: My knowledge in database queries is limited, most of my projects have involved design, and dynamic user interface for scientific applications. Edit: scratch that... but conditional statements are still the way. Someone else will have to help you with the query side of things. Sorry. Quote Link to comment Share on other sites More sharing options...
gorgon2k Posted July 29, 2010 Author Share Posted July 29, 2010 Hi Gorgon, To get the result you're looking for, you'll probably need to throw some javascript in there too. In order to reload the results, you'll need to add something like onClick="this.form.submit()" to your <select> fields. And to set a default, I would look into doing one-line if-else statements to keep your code clean and easy to read (lots of people hate these, but I think they work great when they're used in the right place): <?php $mak = (empty($_GET['mak'])) ? '*' : $_GET['mak']; $mod = (empty($_GET['mod'])) ? '*' : $_GET['mod']; // and so on... ?> This can be thought of as: if $_GET['mak'] is empty or not set, then $mak = '*'; otherwise, $mak = $_GET['mak']; Would something like that work? -jm p.s: My knowledge in database queries is limited, most of my projects have involved design, and dynamic user interface for scientific applications. well i could do that from my script but the thing was, I tested this query SELECT * FROM `automobile_stat` WHERE make = 'Ford' and model = '*' and it doesnt work, I also tried to do * with and without quotes. Quote Link to comment Share on other sites More sharing options...
gorgon2k Posted July 29, 2010 Author Share Posted July 29, 2010 I have all the variable pulled from the forms, I just need a way to query the database to pull the needed info. if I could have a query like this(in basic terms): SELECT * FROM database WHERE make = (if variable has info, use, if not, select all makes) AND model = (if variable has info, use, if not, select all models) AND body_style = = (if variable has info, use, if not, select all body styles) Quote Link to comment Share on other sites More sharing options...
AtlasC1 Posted July 29, 2010 Share Posted July 29, 2010 This is my last attempt, it's good for me too, because I'm also learning. Would something like this work? Note that you will have to name the input fields the exact same as your database table headings. $query = "SELECT * FROM automobile_stat"; if (!empty($_GET)) { $filterString = ' WHERE '; foreach ($_GET as $key => $filter) { $filterString .= $key . " = '" . $filter . "'"; if ($filter != end($_GET)) $filterString .= " AND "; } $query .= $filterString; } $result = mysql_query($query); Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2010 Share Posted July 29, 2010 well i could do that from my script but the thing was, I tested this query SELECT * FROM `automobile_stat` WHERE make = 'Ford' and model = '*' and it doesnt work, I also tried to do * with and without quotes. That query is trying to select any record where make = 'Ford' and there is a literal asterisk in the model field . . . Quote Link to comment Share on other sites More sharing options...
gorgon2k Posted July 30, 2010 Author Share Posted July 30, 2010 OK Guys, thanks, with the usual fooling I GOT IT! here's what i did. I set all the variable to check if they exist or not then plugged them into a query which used the variables. <?PHP $bst = $_GET['bst']; $mak = $_GET['mak']; $mod = $_GET['mod']; $year = $_GET['year']; $check1 = null; if($bst ==""){ $check1 = " body_style LIKE '%'"; } else{ $check1 = " body_style = '".$bst."'"; } $check2 = null; if($mak ==""){ $check2 = " make LIKE '%'"; } else{ $check2 = " make = '".$mak."'"; } $check3 = null; if($mod ==""){ $check3 = " model LIKE '%'"; } else{ $check3 = " model = '".$mod."'"; } $check4 = null; if($year ==""){ $check4 = " model_year LIKE '%'"; } else{ $check4 = " model_year = '".$year."'"; } include('database.php'); ?> <?PHP $fullresult = mysql_query("SELECT * FROM automobile_stat WHERE $check1 and $check2 and $check3 and $check3" ); while($fullrow = mysql_fetch_array($fullresult)){ Quote Link to comment 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.