Jump to content

Help with a automotive sorting code


gorgon2k

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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 . . .

Link to comment
Share on other sites

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)){

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.