boney alex Posted April 18, 2007 Share Posted April 18, 2007 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 Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 18, 2007 Share Posted April 18, 2007 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 Quote Link to comment Share on other sites More sharing options...
boney alex Posted April 18, 2007 Author Share Posted April 18, 2007 Ta mate, works a threat! Thanks 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.