Jump to content


PHP & JavaScript & MySQL Help

  • Please log in to reply
1 reply to this topic

#1 lpxxfaintxx

  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 15 June 2006 - 05:48 PM


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)


Help would be GREATLY appreciated.

P.S. This is what I have so far:

<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>";} ?>
    <td align="right" valign="bottom">&nbsp;</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>";} ?>

#2 nogray

  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 15 June 2006 - 08:46 PM

I assume the $row[0] will return the ID, or you can use $row['You ID field here'] instead

Try the code below, hope it helps

$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";



<script language="javascript">
var subs = new Array();

<?= $sub_arrays ?>

function fill_subs(val){
    if (val != ""){
        var sub_sel = document.getElementById('sub_select');

        opt = document.createElement("OPTION");
        opt.value = "";
        opt.innerHTML = "Select";
        for(i=0; i<subs[val].length; i++){
            opt = document.createElement("OPTION");
            opt.value = subs[val][i];
            opt.innerHTML = subs[val][i];

<select onChange="fill_subs(this.value);">
    <?= $main_cats ?>

<select id="sub_select" name="sub_select">
    <option value="">Select</option>


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users