Jump to content

PHP & JavaScript & MySQL Help


lpxxfaintxx

Recommended Posts

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">&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>";} ?>
      </select>[/code]
Link to comment
Share on other sites

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
[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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.