Hey everyone, I want to use mysql to list all of the makes of vehicles and I want the user to be able to click a make and the years that make were made pop up. Is there a way to do this using PHP? I've got the makes to list out but I can't figure out how to make them into a link that takes you to a page of the all the years it was offered.
Thanks for any help!
Arthur
I would start with something like this, it is untested so may need a little debugging, but not much
<? include("connection.php"); ?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?
$query = "";
if(isset($_GET['make']))
{
if(trim($query) == "")
{
$query .= " where make = '" . mysql_real_escape_string(stripslashes($_GET['make'])) . "'";
}
else
{
$query .= " and make = '" . mysql_real_escape_string(stripslashes($_GET['make'])) . "'";
}
}
if(isset($_GET['year']) && $_GET['year'] != "--select--")
{
if(trim($query) == "")
{
$query .= " where year = '" . mysql_real_escape_string(stripslashes($_GET['year'])) . "'";
}
else
{
$query .= " and year = '" . mysql_real_escape_string(stripslashes($_GET['year'])) . "'";
}
}
$query = "select vehiclepk, make, model, year from vehicles" . $query . " order by make, model";
$searchresult = mysql_query($query);
$query = "select distinct make, year from vehicles order by make, year";
$yearresult = mysql_query($query);
$yeararray = array();
while($row = mysql_fetch_assoc($yearresult))
{
$yeararray[$row["make"]][] = $row["year"];
}
?>
<table cellpadding="0" cellspacing="0">
<?
if(mysql_num_rows($searchresult) == 0 || !is_numeric(mysql_num_rows($searchresult)))
{
?>
<tr>
<td colspan="3">No vehicles found under that search criteria.</td>
</tr>
<?
}
else
{
for($i = 0; $i < mysql_num_rows($searchresult); $i++)
{
?>
<tr>
<td><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?make=<? echo urlencode(mysql_result($searchresult, $i, "make")); ?>"><? echo mysql_result($searchresult, $i, "make"); ?></a></td>
<td><? echo mysql_result($searchresult, $i, "model"); ?></td>
<td>
<form action="<? echo basename($_SERVER['PHP_SELF']); ?>" method="get">
<input type="hidden" name="make" value="<? echo mysql_result($searchresult, $i, "make"); ?>" />
<select name="year" id="year<? echo mysql_result($searchresult, $i, "vehiclepk"); ?>">
<option value="--select--">--select--</option>
<?
foreach($yeararray[mysql_result($searchresult, $i, "make")] as $value)
{
?>
<option value="<? echo $value; ?>"><? echo $value; ?></option>
<?
}
?>
</select>
<input type="submit" value="Search" />
</form>
</td>
</tr>
<?
}
}
?>
</table>
</body>
</html>
<?
mysql_close();
?>
just edited the second loop