I'm currently trying to set up a search with a couple of dynamic dropdowns, basically to search for cars - so the parent list is car make, and the child is car model.
I've gotten as far as a page with the dynamic array working, but not the search :
http://www.mph.uk.com/leasing101.php
(Ignore the cars and vans radio buttons)
For testing there's an Audi A3 and and Audi A6, but this search doesn't work.
The select code looks like :
<select name="Model">
<option value="" <?php if (!(strcmp("", $row_rsModels['Model']))) {echo "selected=\"selected\"";}
?>>Select a Model</option>
</select>
And the array code looks like :
<?php
if ($row_rsModels) {
echo "<SC" . "RIPT>\n";
echo "var WAJA = new Array();\n";
$oldmainid = 0;
$newmainid = $row_rsModels["Make"];
if ($oldmainid == $newmainid) {
$oldmainid = "";
}
$n = 0;
while ($row_rsModels) {
if ($oldmainid != $newmainid) {
echo "WAJA[".$n."] = new Array();\n";
echo "WAJA[".$n."][0] = '".WA_DD_Replace($newmainid)."';\n";
$m = 1;
}
echo "WAJA[".$n."][".$m."] = new Array();\n";
echo "WAJA[".$n."][".$m."][0] = "."'".WA_DD_Replace($row_rsModels["ModelID"])."'".";\n";
echo "WAJA[".$n."][".$m."][1] = "."'".WA_DD_Replace($row_rsModels["Model"])."'".";\n";
$m++;
if ($oldmainid == 0) {
$oldmainid = $newmainid;
}
$oldmainid = $newmainid;
$row_rsModels = mysql_fetch_assoc($rsModels);
if ($row_rsModels) {
$newmainid = $row_rsModels["Make"];
}
if ($oldmainid != $newmainid) {
$n++;
}
}
echo "var rsModels_WAJA = WAJA;\n";
echo "WAJA = null;\n";
echo "</SC" . "RIPT>\n";
}
function WA_DD_Replace($startStr) {
$startStr = str_replace("'", "|WA|", $startStr);
$startStr = str_replace("\\", "\\\\", $startStr);
$startStr = preg_replace("/[\r\n]{1,}/", " ", $startStr);
return $startStr;
}
?>
..to try and find the root of the problem, I've gotten as far as a similar page, without the dynamic
array, with the child dropdown populated dynaically from the recordset :
http://www.mph.uk.com/leasing501.php
This seems to work exactly as advertised - all the car models are listed from the recordset, and if
you search for Audi and A3, then it gets returned correctly.
The select code here looks like :
<select name="Model" id="Model">
<option value="">Select a Model</option>
<?php
do {
?>
<option value="<?php echo $row_rsModels['Model']?>"><?php echo $row_rsModels['Model']?
</option>
<?php
} while ($row_rsModels = mysql_fetch_assoc($rsModels));
$rows = mysql_num_rows($rsModels);
if($rows > 0) {
mysql_data_seek($rsModels, 0);
$row_rsModels = mysql_fetch_assoc($rsModels);
}
?>
</select>
..so I guess the search is set up OK, but the issue is with how to pass the model from the select code generated by the dynamic array correctly....
Its presumably because the option value is empty :
<option value="" <?php if (!(strcmp("", $row_rsModels['Model']))) {echo "selected=\"selected\"";} ?>>Select a Model</option>
So I've been trying various possibilities with no joy, such as :
<?php if (!(strcmp("", $row_rsModels['Model']))) {echo "selected=\"selected\"";} ?>
but without success.
Even then, I'd have though if the value was empty, "", then it would return all results, rather than no results....?
hope that makes some sense, and someone can shed some light...
Cheers.