PDA

View Full Version : Dropdown menu to search


Narth
08-24-2009, 01:59 PM
Okay so i've followed the dropdown menu to the letter just adding more makes and models, and all is working fine, but im trying to get the values it posts to be searched within a third table, which ive called make_model

So, if you were to select BMW, it refreshes giving all the BMW's, and if you were to select 3 series it would take BMW and 3 SERIES and give you all the BMW 3 Series...

So far, i've tried using "GET" and "POST" to do this, and all its doing it automatically fetching EVERYTHING out of the make_model table

I also linked it to a submit button, but this just brings up nothing when you submit

Any help? I've looked into usings sessions too but not had any luck with this

This is what i have so far

<?php
///////////////////////////////// CONNECTION
require_once("Connections/connection.php");

$make = $_POST['make'];

if ($make){

$query = sprintf("SELECT * FROM car_model where car_model_id='$make'");
$result = @mysql_query($query);
$rowModel = mysql_fetch_array($result);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>
</head>
<body>


<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">

<select name="make" onChange="document.forms[0].submit()">
<option value="" selected="selected">Select Make</option>
<option value="1" <?php if(!(strcmp(1, $make))){echo "selected";}?>>AC</option>
<option value="2" <?php if(!(strcmp(2, $make))){echo "selected";}?>>ALFA</option>
<option value="3" <?php if(!(strcmp(3, $make))){echo "selected";}?>>AUDI</option>
<option value="4" <?php if(!(strcmp(4, $make))){echo "selected";}?>>AUSTIN</option>
<option value="5" <?php if(!(strcmp(5, $make))){echo "selected";}?>>AUSTIN-HEALEY</option>
<option value="6" <?php if(!(strcmp(6, $make))){echo "selected";}?>>BMW</option>
<option value="7" <?php if(!(strcmp(7, $make))){echo "selected";}?>>CATERHAM</option>
<option value="8" <?php if(!(strcmp(8, $make))){echo "selected";}?>>CHEVROLET</option>
<option value="9" <?php if(!(strcmp(9, $make))){echo "selected";}?>>CHRYSLER</option>
<option value="10" <?php if(!(strcmp(10, $make))){echo "selected";}?>>CITROEN</option>
<option value="11" <?php if(!(strcmp(11, $make))){echo "selected";}?>>DAEWOO</option>
<option value="12" <?php if(!(strcmp(12, $make))){echo "selected";}?>>DAIHATSU</option>
<option value="13" <?php if(!(strcmp(13, $make))){echo "selected";}?>>FIAT</option>
<option value="14" <?php if(!(strcmp(14, $make))){echo "selected";}?>>FORD</option>
<option value="15" <?php if(!(strcmp(15, $make))){echo "selected";}?>>HONDA</option>
<option value="16" <?php if(!(strcmp(16, $make))){echo "selected";}?>>HYUNDAI</option>
<option value="17" <?php if(!(strcmp(17, $make))){echo "selected";}?>>ISUZU</option>
<option value="18" <?php if(!(strcmp(18, $make))){echo "selected";}?>>IVECO</option>
<option value="19" <?php if(!(strcmp(19, $make))){echo "selected";}?>>JAGUAR</option>
<option value="20" <?php if(!(strcmp(20, $make))){echo "selected";}?>>JEEP</option>
<option value="21" <?php if(!(strcmp(21, $make))){echo "selected";}?>>KIA</option>
<option value="22" <?php if(!(strcmp(22, $make))){echo "selected";}?>>LANCIA</option>
<option value="23" <?php if(!(strcmp(23, $make))){echo "selected";}?>>LAND-ROVER</option>
<option value="24" <?php if(!(strcmp(24, $make))){echo "selected";}?>>LDV</option>
<option value="25" <?php if(!(strcmp(25, $make))){echo "selected";}?>>LEXUS</option>
<option value="26" <?php if(!(strcmp(26, $make))){echo "selected";}?>>LOTUS</option>
<option value="27" <?php if(!(strcmp(27, $make))){echo "selected";}?>>MAZDA</option>
<option value="28" <?php if(!(strcmp(28, $make))){echo "selected";}?>>MCC</option>
<option value="29" <?php if(!(strcmp(29, $make))){echo "selected";}?>>MERCEDES-BENZ</option>
<option value="30" <?php if(!(strcmp(30, $make))){echo "selected";}?>>MG</option>
<option value="31" <?php if(!(strcmp(31, $make))){echo "selected";}?>>MINI</option>
<option value="32" <?php if(!(strcmp(32, $make))){echo "selected";}?>>MITSUBISHI</option>
<option value="33" <?php if(!(strcmp(33, $make))){echo "selected";}?>>NISSAN</option>
<option value="34" <?php if(!(strcmp(34, $make))){echo "selected";}?>>OPEL</option>
<option value="35" <?php if(!(strcmp(35, $make))){echo "selected";}?>>PERODUA</option>
<option value="36" <?php if(!(strcmp(36, $make))){echo "selected";}?>>PEUGEOT</option>
<option value="37" <?php if(!(strcmp(37, $make))){echo "selected";}?>>PORSCHE</option>
<option value="38" <?php if(!(strcmp(38, $make))){echo "selected";}?>>PROTON</option>
<option value="39" <?php if(!(strcmp(39, $make))){echo "selected";}?>>RELIANT</option>
<option value="40" <?php if(!(strcmp(40, $make))){echo "selected";}?>>RENAULT</option>
<option value="41" <?php if(!(strcmp(41, $make))){echo "selected";}?>>ROVER</option>
<option value="42" <?php if(!(strcmp(42, $make))){echo "selected";}?>>SAAB</option>
<option value="43" <?php if(!(strcmp(43, $make))){echo "selected";}?>>SEAT</option>
<option value="44" <?php if(!(strcmp(44, $make))){echo "selected";}?>>SKODA</option>
<option value="45" <?php if(!(strcmp(45, $make))){echo "selected";}?>>SSANGYONG</option>
<option value="46" <?php if(!(strcmp(46, $make))){echo "selected";}?>>SUBARU</option>
<option value="47" <?php if(!(strcmp(47, $make))){echo "selected";}?>>SUZUKI</option>
<option value="48" <?php if(!(strcmp(48, $make))){echo "selected";}?>>TALBOT</option>
<option value="49" <?php if(!(strcmp(49, $make))){echo "selected";}?>>TOYOTA</option>
<option value="50" <?php if(!(strcmp(50, $make))){echo "selected";}?>>TRIUMPH</option>
<option value="51" <?php if(!(strcmp(51, $make))){echo "selected";}?>>TVR</option>
<option value="52" <?php if(!(strcmp(52, $make))){echo "selected";}?>>VAUXHALL</option>
<option value="53" <?php if(!(strcmp(53, $make))){echo "selected";}?>>VOLVO</option>
<option value="54" <?php if(!(strcmp(54, $make))){echo "selected";}?>>VOLKSWAGEN</option>
<option value="55" <?php if(!(strcmp(55, $make))){echo "selected";}?>>WESTFIELD</option>
</select>

<select name="model">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option>
<?php }while ($rowModel = mysql_fetch_array($result)); ?>
</select>
<input type='submit' value='submit' />
</form>
<?

if (isset($_GET['submit'])) {
$model = $_GET['model'];
$make = $_GET['make'];

echo "You Searched for $make $model";

$querys = sprintf("SELECT * FROM make_model WHERE $make='Make' AND $model='Model'"); //selects all data from the database
$results = @mysql_query($querys); //tells the database
$row = mysql_fetch_array($results); //fetches the result
$querys = " ORDER BY `Model`";

$searchSQL = "SELECT ID, Make, Model, Detail, Capacity, Year FROM make_model WHERE ";

// this is where i grab the search types
$types = array();
$types[] = isset($_GET['mfinder'])?"`model` LIKE '%{$mfinder}%'":'';

$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)

if (count($types) < 1)
$types[] = "`Model` LIKE '%{$mfinder}%'"; // use the model as a default search if none are checked

$andOr = isset($_GET['matchall'])?'AND':'OR';
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `id`"; // order by id

$searchResult = mysql_query($searchSQL) or die("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");

$size = 0;
$cnt = 0;

"</Center></td>";
echo "<tr>";
echo "<table border='0' width '955' align=center>";
echo "</tr>";
echo "<br>";

echo "<table border='1' width='955' align=center>";

echo "<tr>";
echo "<td><b>Make</b></td>";
echo "<td><b>Model</b></td>";
echo "<td><b>Detail</b></td>";
echo "<td><b>Capacity</b></td>";
echo "<td><b>Year</b></td>";

echo "</tr>";
while ($row1 = mysql_fetch_array($searchResult)) {

echo "<tr>";
echo "<td>".$row1['Make']."</td>";
echo "<td>".$row1['Model']."</td>";
echo "<td>".$row1['Detail']."</td>";
echo "<td>".$row1['Capacity']."</td>";
echo "<td>".$row1['Year']."</td>";
}
$cnt++;
echo "</tr>";
echo "</table>";
echo "<br><br><br><br><br>";


}

function removeEmpty($var) {
return (!empty($var));
}

?>
</p>

Narth
08-24-2009, 10:17 PM
An update, i've managed to figure out how to get the Make to post, however can't for the life of me manage to get Model to post, when i set the POST variable it just fetches the make again..

Any help would be appreciated!

<?php
///////////////////////////////// CONNECTION
require_once("Connections/connection.php");
session_start();
$make = $_POST['make'];

if ($make){

$query = sprintf("SELECT * FROM car_model where car_model_id='$make'");
$result = @mysql_query($query);
$rowModel = mysql_fetch_array($result);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>
</head>
<body>
<br />
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">

<select name="make" onChange="document.forms[0].submit()">
<option value="" selected="selected">Select Make</option>
<option value="AC" <?php if(!(strcmp(AC, $make))){echo "selected";}?>>AC</option>
<option value="ALFA" <?php if(!(strcmp(ALFA, $make))){echo "selected";}?>>ALFA</option>
<option value="AUDI" <?php if(!(strcmp(AUDI, $make))){echo "selected";}?>>AUDI</option>
<option value="AUSTIN" <?php if(!(strcmp(AUSTIN, $make))){echo "selected";}?>>AUSTIN</option>
<option value="AUSTIN-HEALEY" <?php if(!(strcmp(AUSTIN-HEALEY, $make))){echo "selected";}?>>AUSTIN-HEALEY</option>
<option value="BMW" <?php if(!(strcmp(BMW, $make))){echo "selected";}?>>BMW</option>
<option value="CATERHAM" <?php if(!(strcmp(CATERHAM, $make))){echo "selected";}?>>CATERHAM</option>
<option value="CHEVROLET" <?php if(!(strcmp(CHEVROLET, $make))){echo "selected";}?>>CHEVROLET</option>
<option value="CHRYSLER" <?php if(!(strcmp(CHRYSLER, $make))){echo "selected";}?>>CHRYSLER</option>
<option value="CITROEN" <?php if(!(strcmp(CITROEN, $make))){echo "selected";}?>>CITROEN</option>
<option value="DAEWOO" <?php if(!(strcmp(DAEWOO, $make))){echo "selected";}?>>DAEWOO</option>
<option value="DAIHATSU" <?php if(!(strcmp(DAIHATSU, $make))){echo "selected";}?>>DAIHATSU</option>
<option value="FIAT" <?php if(!(strcmp(FIAT, $make))){echo "selected";}?>>FIAT</option>
<option value="FORD" <?php if(!(strcmp(FORD, $make))){echo "selected";}?>>FORD</option>
<option value="HONDA" <?php if(!(strcmp(HONDA, $make))){echo "selected";}?>>HONDA</option>
<option value="HYUNDAI" <?php if(!(strcmp(HYUNDAI, $make))){echo "selected";}?>>HYUNDAI</option>
<option value="ISUZU" <?php if(!(strcmp(ISUZU, $make))){echo "selected";}?>>ISUZU</option>
<option value="IVECO" <?php if(!(strcmp(IVECO, $make))){echo "selected";}?>>IVECO</option>
<option value="JAGUAR" <?php if(!(strcmp(JAGUAR, $make))){echo "selected";}?>>JAGUAR</option>
<option value="JEEP" <?php if(!(strcmp(JEEP, $make))){echo "selected";}?>>JEEP</option>
<option value="JEEP" <?php if(!(strcmp(JEEP, $make))){echo "selected";}?>>JEEPA</option>
<option value="LANCIA" <?php if(!(strcmp(LANCIA, $make))){echo "selected";}?>>LANCIA</option>
<option value="LAND-ROVER" <?php if(!(strcmp(LAND-ROVER, $make))){echo "selected";}?>>LAND-ROVER</option>
<option value="LDV" <?php if(!(strcmp(LDV, $make))){echo "selected";}?>>LDV</option>
<option value="LEXUS" <?php if(!(strcmp(LEXUS, $make))){echo "selected";}?>>LEXUS</option>
<option value="LOTUS" <?php if(!(strcmp(LOTUS, $make))){echo "selected";}?>>LOTUS</option>
<option value="MAZDA" <?php if(!(strcmp(MAZDA, $make))){echo "selected";}?>>MAZDA</option>
<option value="MCC" <?php if(!(strcmp(MCC, $make))){echo "selected";}?>>MCC</option>
<option value="MERCEDES-BENZ" <?php if(!(strcmp(MERCEDES-BENZ, $make))){echo "selected";}?>>MERCEDES-BENZ</option>
<option value="MG" <?php if(!(strcmp(MG, $make))){echo "selected";}?>>MG</option>
<option value="MINI" <?php if(!(strcmp(MINI, $make))){echo "selected";}?>>MINI</option>
<option value="MITSUBISHI" <?php if(!(strcmp(MITSUBISHI, $make))){echo "selected";}?>>MITSUBISHI</option>
<option value="NISSAN" <?php if(!(strcmp(NISSAN, $make))){echo "selected";}?>>NISSAN</option>
<option value="OPEL" <?php if(!(strcmp(OPEL, $make))){echo "selected";}?>>OPEL</option>
<option value="PERODUA" <?php if(!(strcmp(PERODUA, $make))){echo "selected";}?>>PERODUA</option>
<option value="PEUGEOT" <?php if(!(strcmp(PEUGEOT, $make))){echo "selected";}?>>PEUGEOT</option>
<option value="PORSCHE" <?php if(!(strcmp(PORSCHE, $make))){echo "selected";}?>>PORSCHE</option>
<option value="PROTON" <?php if(!(strcmp(PROTON, $make))){echo "selected";}?>>PROTON</option>
<option value="RELIANT" <?php if(!(strcmp(RELIANT, $make))){echo "selected";}?>>RELIANT</option>
<option value="RENAULT" <?php if(!(strcmp(RENAULT, $make))){echo "selected";}?>>RENAULT</option>
<option value="ROVER" <?php if(!(strcmp(ROVER, $make))){echo "selected";}?>>ROVER</option>
<option value="SAAB" <?php if(!(strcmp(SAAB, $make))){echo "selected";}?>>SAAB</option>
<option value="SEAT" <?php if(!(strcmp(SEAT, $make))){echo "selected";}?>>SEAT</option>
<option value="SKODA" <?php if(!(strcmp(SKODA, $make))){echo "selected";}?>>SKODA</option>
<option value="SSANGYONG" <?php if(!(strcmp(SSANGYONG, $make))){echo "selected";}?>>SSANGYONG</option>
<option value="SUBARU" <?php if(!(strcmp(SUBARU, $make))){echo "selected";}?>>SUBARU</option>
<option value="SUZUKI" <?php if(!(strcmp(SUZUKI, $make))){echo "selected";}?>>SUZUKI</option>
<option value="TALBOT" <?php if(!(strcmp(TALBOT, $make))){echo "selected";}?>>TALBOT</option>
<option value="TOYOTA" <?php if(!(strcmp(TOYOTA, $make))){echo "selected";}?>>TOYOTA</option>
<option value="TRIUMPH" <?php if(!(strcmp(TRIUMPH, $make))){echo "selected";}?>>TRIUMPH</option>
<option value="TVR" <?php if(!(strcmp(TVR, $make))){echo "selected";}?>>TVR</option>
<option value="VAUXHALL" <?php if(!(strcmp(VAUXHALL, $make))){echo "selected";}?>>VAUXHALL</option>
<option value="VOLVO" <?php if(!(strcmp(VOLVO, $make))){echo "selected";}?>>VOLVO</option>
<option value="VOLKSWAGEN" <?php if(!(strcmp(VOLKSWAGEN, $make))){echo "selected";}?>>VOLKSWAGEN</option>
<option value="WESTFIELD" <?php if(!(strcmp(WESTFIELD, $make))){echo "selected";}?>>WESTFIELD</option>
</select>
<?
$makech = $_POST['make']; ?>
<select name="model">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option>
<?php }while ($rowModel = mysql_fetch_array($result)); ?>
</select>
<?php
?>
<input class="button" type="submit" name="search" value="Search" />
</form>
<br />
<?
if (isset($_POST['model'])){

$model = $_POST['model'];
echo $makech; ?><br><?
echo $model; ?><br /><?
}
?>