Selecting from a database in a sub category when a category has been chosen in a form

ggeoff

New Member
I currently have this form - a snippet of which is below:\[code\] <li> <label for="Catid">Pick a Category:</label> <select name="Catid"> <?php $Products = New Products(); $Products->form_Cat_Picker(); ?> </select> </li> <li> <label for="Subcatid">Sub Category:</label> <select name="Subcatid"> <?php $Products = New Products(); $Products->form_Subcat_Picker(); ?> </select>\[/code\]Ok, so this then sends to products.php which in the products class does this:\[code\]function form_Cat_Picker() { $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME); if (!$mysqli) { die('There was a problem connecting to the database.'); } $catPicker = "SELECT Catid, Catname FROM ProductCats ORDER BY Catid"; if ($Result = $mysqli->query($catPicker)){ if (!$Result) { echo 'Could not run query: ' . mysql_error(); exit; } else { $Result = $mysqli->query($catPicker); } while ($row = $Result->fetch_assoc()) { echo '<option value="'.$row["Catid"].'">'.$row["Catname"]."</option>"; } } $mysqli->close();}function form_Subcat_Picker() { $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME); if (!$mysqli) { die('There was a problem connecting to the database.'); } $catPicker = "SELECT Subcatid, Subcatname, Parentid FROM ProductSubCats ORDER BY Subcatid"; if ($Result = $mysqli->query($catPicker)){ if (!$Result) { echo 'Could not run query: ' . mysql_error(); exit; } else { $Result = $mysqli->query($catPicker); } while ($row = $Result->fetch_assoc()) { echo '<option value="'.$row["Subcatid"].'">'.$row["Subcatname"]."</option>"; } } $mysqli->close();}\[/code\]Both of these, essentially return a list of categories and sub categories from the database, to populate the form's drop down list.The problem is, I don't want the Sub categories to appear unless their parent was selected in the initial dropdown box. How can I achieve this? I currently have an extra column in the table ProductSubCats which is Parentid, and one in ProductCats which is Catid, and they connect up.But how would I set a condition firstly to create a variable where an option/value is selected, and secondly to say "this variable $selected".I would know how to pass it through to that code and use a condition WHERE $selected = whatever, but it is getting to that point that confuses me. Thank youEDIT - it seems my error is in forgetting php will run once on page load.So I should have it present all of the data and then use jquery to only display those that have been selected. But I should also have a value of 0 and a blank entry as well. if for example I have a category with the value 1 and 2 sub categories with the parent id of 1 How would I have jquery show those subcats with the parent id of 1, and not say, those with the parent id of 2? After user selection?
 
Back
Top