Jump to content

why will this list not populate?


jbrill

Recommended Posts

k guys, im getting really frustrated with this script...

I would like to be able to select the "category" and then have the sub category list populate with only the subcategories that relate to each category.

here is my code...

<?php
include '../includes/dbconnect.php';

function loadsubcat($a)
{
// spot for entering data for all cities of each province detected within the database

$loadsub = "SELECT subcat FROM subcategory WHERE cat='".$a."'";
$clist = mysql_query($loadsub);
$subcats = mysql_fetch_array($clist);

do
{
echo "\"('".$subcats["subcat"]."')\",";
}
while($subcats = mysql_fetch_array($clist));

}

?>
<html>
<head>
<title></title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin


<?

$catsql = "SELECT DISTINCT cat FROM category";
$cats = mysql_query($catsql);
$category = mysql_fetch_array($cats);

do 

{
echo "var ".$category["cat"]."Array =  new Array(\"('Select Sub-Category','',true,true)\",";
loadsubcat($category["cat"]);
echo "\"('')\"";
echo ");\n";

} while ($category = mysql_fetch_array($cats));

?>


function populateSubcat(inForm,selected) {
var selectedArray = eval(selected + "Array");
while (selectedArray.length < inForm.Subcat.options.length) {
inForm.Subcat.options[(inForm.Subcat.options.length - 1)] = null;
}
for (var i=0; i < selectedArray.length; i++) {
eval("inForm.Subcat.options[i]=" + "new Option" + selectedArray[i]);
}
if (inForm.cat.options[0].value == '') {
inForm.cat.options[0]= null;
if ( navigator.appName == 'Netscape') {
if (parseInt(navigator.appVersion) < 4) {
window.history.go(0);
}
else {   	
if (navigator.platform == 'Win32' || navigator.platform == 'Win16') {
window.history.go(0);
}
}
}
}
}

</script>
</head>
<body>

<select name="cat" onChange="populateSubcat(document.categories,document.categories.cat.options[document.categories.cat.selectedIndex].value)">
<option selected value=''>Select Category</option>
<?

$catsql = "SELECT DISTINCT cat, category FROM category";
$cats = mysql_query($catsql);
$category = mysql_fetch_array($cats);

do

{
echo "<option value='".$category["cat"]."'>".$category["category"]."</option>";


} while ($category = mysql_fetch_array($cats));



?>
</select>



<font class=input>Sub Category:
</td>
<td>
	<select name="Subcat" onChange="populateUSstate(document.categories,document.categories.Subcat.options[document.categories.Subcat.selectedIndex].text)">
<option value=''>Select Category First</option>



</body>
</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/60610-why-will-this-list-not-populate/
Share on other sites

I changed you do-while section to this:

<?php

$catsql = "SELECT DISTINCT cat FROM category";
$cats = mysql_query($catsql);
while($category = mysql_fetch_array($cats)){
echo "var ".$category["cat"]."Array =  new Array(\"('Select Sub-Category','',true,true)\",";
loadsubcat($category["cat"]);
echo "\"('')\"";
echo ");\n";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.