So I've needed this functionality for some time and finally got something to work using the chained select code from yxscripts.com and some basic PHP. Now the code is a little sloppy so if anyone would like to help clean it up before it is released as a contribution that'd be great.
This is the DB Heirarchy I'm using:
-> Make
----> Model
-------> Years
For the years, I'm using ranges for specific generations.
Here is the JS/PHP content for the document head:
<!-- Start Navigation -->
<script language="javascript" src="chainedselects.js">
/***********************************************
* Chained Selects script- By Xin Yang (http://www.yxscripts.com/)
* Script featured on/available at http://www.dynamicdrive.com/
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
</script>
<?php
echo "
<script language=\"javascript\">
//var hide_empty_list=true; //uncomment this line to hide empty selection lists
var disable_empty_list=true; //uncomment this line to disable empty selection lists
addListGroup(\"vehicles\", \"make\");\n
addOption(\"make\", \"Select a Make\", \"Select a Make\", \"\"); //Empty starter option
";
$make = "SELECT categories.categories_id, categories_name FROM categories, categories_description WHERE categories.parent_id = '0' AND categories_description.categories_id = categories.categories_id ORDER BY sort_order";
$makeResult = mysql_query($make);
while ($makeRow = mysql_fetch_array($makeResult)){
$mkid = $makeRow['categories_id'];
$mkname = $makeRow['categories_name'];
echo "addList(\"make\", \"" . $mkname . "\", \"" . $mkid . "\", \"" . $mkname . "\");
";
echo "addOption(\"" . $mkname . "\", \"Select a Model\", \"Select a Model\", \"\"); //Empty starter option
";
$model = "SELECT categories.categories_id, categories_name FROM categories, categories_description WHERE categories.parent_id = '" . $mkid . "' AND categories_description.categories_id = categories.categories_id ORDER BY sort_order";
$modelResult = mysql_query($model);
while ($modelRow = mysql_fetch_array($modelResult)){
$mdid = $modelRow['categories_id'];
$mdname = $modelRow['categories_name'];
echo "addList(\"" . $mkname . "\", \"" . $mdname . "\", \"" . $mdid . "\", \"" . $mdname . "\");
";
echo "addOption(\"" . $mdname . "\", \"Select a year\", \"\", 1);
";
$year = "SELECT categories.categories_id, categories_name FROM categories, categories_description WHERE categories.parent_id = '" . $mdid . "' AND categories_description.categories_id = categories.categories_id ORDER BY sort_order";
$yearResult = mysql_query($year);
while ($yearRow = mysql_fetch_array($yearResult)){
$yrid = $yearRow['categories_id'];
$yrname = $yearRow['categories_name'];
echo "addOption(\"" . $mdname . "\", \"" . $yrname . "\", \"http://www.gforceauto.com/index.php?cPath=" . $mkid . "_" . $mdid . "_" . $yrid . "\");
";
}
}
}
echo "</script>";
?>
<script language="javascript">
function openLink(url) {
if (url != "") {
location.href = url;
}
else {
alert("Please select a site.");
}
}
</script>
<!-- End Navigation -->
Also, this script has cookie functionality so the selections will stick after visiting different pages, so the <BODY> tag needs the following:
<body onload="initListGroup('vehicles', document.forms[0].make, document.forms[0].model, document.forms[0].year, 'cs')">
Now the form for the categories box:
<form>
<table align=\"center\">
<tr>
<td><select name=\"make\" style=\"width:160px;\"></select></td>
</tr>
<tr>
<td><select name=\"model\" style=\"width:160px;\"></select></td>
</tr>
<tr>
<td><select name=\"year\" style=\"width:160px;\" onchange=\"openLink(this.value)\"></select></td>
</tr>
</table>
</form>
I'm using BTS so my categories.php looks like this:
<?php
/*
$Id: categories.php,v 1.24 2003/06/09 22:09:55 hpdl Exp $
modified by paulm_nl 2003/12/23
osCommerce, Open Source E-Commerce Solutions
http://www.oscommerce.com
Copyright (c) 2003 osCommerce
Released under the GNU General Public License
*/
$boxHeading = BOX_HEADING_CATEGORIES;
$corner_left = 'rounded';
$corner_right = 'square';
$box_base_name = 'categories'; // for easy unique box template setup (added BTSv1.2)
$box_id = $box_base_name . 'Box'; // for CSS styling paulm (editted BTSv1.2)
$boxContent .= "<form>
<table align=\"center\">
<tr>
<td><select name=\"make\" style=\"width:160px;\"></select></td>
</tr>
<tr>
<td><select name=\"model\" style=\"width:160px;\"></select></td>
</tr>
<tr>
<td><select name=\"year\" style=\"width:160px;\" onchange=\"openLink(this.value)\"></select></td>
</tr>
</table>
</form>";
include (bts_select('boxes', $box_base_name)); // BTS 1.5
?>
You may see the menu in action here: http://www.gforceauto.com (I haven't even started templating anything, nor do I have all makes and models entered etc.)
Have fun and any input is appreciated.
Thanks,
Adam