jbrill Posted July 18, 2007 Share Posted July 18, 2007 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> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 18, 2007 Share Posted July 18, 2007 is it because you are commenting it out with this: <!-- Begin and then never closing it with this --> ??? Quote Link to comment Share on other sites More sharing options...
suttercain Posted July 18, 2007 Share Posted July 18, 2007 Are the short tags on? You may have to use <?php instead of <? Quote Link to comment Share on other sites More sharing options...
jbrill Posted July 18, 2007 Author Share Posted July 18, 2007 took out the comment part, still doesnt work. short tags are on so technically it should work? ??? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 18, 2007 Share Posted July 18, 2007 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"; } ?> Quote Link to comment Share on other sites More sharing options...
jbrill Posted July 18, 2007 Author Share Posted July 18, 2007 changing the do part didnt do anythign but make no categories appear either. Quote Link to comment Share on other sites More sharing options...
suttercain Posted July 24, 2007 Share Posted July 24, 2007 Short tags should work... but I would recommend trying <?php instead of <? just to be safe. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.