lpxxfaintxx Posted June 15, 2006 Share Posted June 15, 2006 Hello,I have 2 MySQL tables, 'Main Category', and 'Sub-Category.'Basically, the Main Category is the main/parent category, and the Sub-Category is a child of the main. In Sub-Category, there's 3 fields, subid, maincatid, and name. What I want is 2 dynamic dropdown menus. The first one, makes a query and gets all the Main Categories AND all the rows. Then, another query is made. The new query gets all the Sub-Categories thats field, 'maincatid' is the chosen one from the first drop-down menu. The second drop-down menu is updated, grabbing all of the correct Sub-Categories. Sorry, I am a bad explainer. If you are confused on what I am saying, please contact me through either this thread, my AIM, or my MSN. (All in my profile)Regards,Help would be GREATLY appreciated.P.S. This is what I have so far: [code]<select name="category"> <option value="Default">Choose A Category:</option> <?php $query = "SELECT * FROM category"; $result = mysql_query ($query); while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {echo "<option value='{$row[0]}'>{$row[0]}</option>";} ?> </select></label></td> </tr> <tr> <td align="right" valign="bottom"> </td> <td><select name="subcategory"> <option value="Default">Choose A Sub-Category:</option> <?php $query = "SELECT * FROM subcategory WHERE maincatid = 'AHHHH! Help!'"; $result = mysql_query ($query); while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {echo "<option value='{$row[0]}'>{$row[0]}</option>";} ?> </select>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12086-php-javascript-mysql-help/ Share on other sites More sharing options...
nogray Posted June 15, 2006 Share Posted June 15, 2006 I assume the $row[0] will return the ID, or you can use $row['You ID field here'] insteadTry the code below, hope it helps[code]<?php$query = "SELECT * FROM category";$result = mysql_query ($query);while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $main_cats .= "<option value='".$row[0]."'>".$row[0]."</option>"; $query1 = "SELECT * FROM subcategory WHERE maincatid = '".$row[0]."'"; $result1 = mysql_query ($query1); $sub_arrays .= "subs[".$row[0]."] = new Array("; while ($row1 = mysql_fetch_array($result1, MYSQL_NUM)) { $sub_arrays .= "\"".$row1[0]."\","; } $sub_arrays = trim($sub_arrays, ",") .");\n";}?><html><head><script language="javascript">var subs = new Array();<?= $sub_arrays ?>function fill_subs(val){ if (val != ""){ var sub_sel = document.getElementById('sub_select'); sub_sel.innerHTML=""; opt = document.createElement("OPTION"); opt.value = ""; opt.innerHTML = "Select"; sub_sel.appendChild(opt); for(i=0; i<subs[val].length; i++){ opt = document.createElement("OPTION"); opt.value = subs[val][i]; opt.innerHTML = subs[val][i]; sub_sel.appendChild(opt); } }}</script></head><body><select onChange="fill_subs(this.value);"> <?= $main_cats ?></select><select id="sub_select" name="sub_select"> <option value="">Select</option></select></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12086-php-javascript-mysql-help/#findComment-46078 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.