Jump to content

[SOLVED] Help with a drop down filter


boney alex

Recommended Posts

Hi, I've got two drop down menus that I'm using to filter some data. It works when the user selects an option from both menus but always return ZERO records when only one of out the two menus have been set. My code POSTS the ID from each menu then puts that ID into the WHERE clause of an SQL query.

<?php
$makemodelID = $_POST['vantype'];
$branchID = $_POST['branch'];

if($query= mysql_query("SELECT VehicleID, Registration, Make, Model, Specification, Colour, Mileage, Transmission, Branch_Name FROM Vehicle LEFT JOIN MakeandModel ON MakeandModel.MakeModelID = Vehicle.MakeModelID LEFT JOIN Branch ON Vehicle.BranchID = Branch.BranchID WHERE Vehicle.MakeModelID = '$makemodelID' AND Vehicle.BranchID = '$branchID' ORDER BY Make, Model, Specification, Mileage")) 
?>

 

I think the problem lies with the makemodelID = ' ' or the branchID = ' ', which automatically returns no records. Is there anyway around this?

 

Cheers

Link to comment
Share on other sites

you could put on POST page

 

if (($makemodelID == "")&&($branchID == "")) //neither have a value
{
$queryadd = "ORDER BY Make, Model, Specification, Mileage"; // just the order by statement
}
elseif(($makemodelID != "")&&($branchID == "")) //only $makemodel has a value
{
$queryadd = "WHERE Vehicle.MakeModelID = '$makemodelID' ORDER BY Make, Model, Specification, Mileage";
}
elseif(($makemodelID == "")&&($branchID != "")) // only $branchID has a value
{
$queryadd = "WHERE Vehicle.BranchID = '$branchID' ORDER BY Make, Model, Specification, Mileage";
}
else //both have a value
{
$queryadd = "WHERE Vehicle.MakeModelID = '$makemodelID' AND Vehicle.BranchID = '$branchID' ORDER BY Make, Model, Specification, Mileage";
}

$queryall = "SELECT VehicleID, Registration, Make, Model, Specification, Colour, Mileage, Transmission, Branch_Name FROM Vehicle 
LEFT JOIN MakeandModel ON MakeandModel.MakeModelID = Vehicle.MakeModelID LEFT JOIN 
Branch ON Vehicle.BranchID = Branch.BranchID $queryadd"; // add $queryadd to the end of the query

if($query= mysql_query($queryall) or die ("Error in query " . mysql_error()); //run the query

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.