Jump to content

pulldown menu that creates a sub pulldown menu


gateway69

Recommended Posts

Im trying to write a function that pulls data from 2 tables in a database.

categories and subcategories which has a pointer to categories so you know sub is linked to which category.

[code]
<?php
/// List categories for add video pull down category menus
function viewcategories($userlevel){
  global $database;
  $q = "SELECT categories.id,categories.name,subcategories.id,subcategories.name from categories,subcategories WHERE subcategories.catid = categories.id and categories.active ='1' and categories.disable='1' and subcategories.active ='1' and subcategories.disable='1' ORDER by categories.id";
 
  $result = $database->query($q);
  /* Error occurred, return given name by default */
  $num_rows = mysql_numrows($result);
  if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
  }
  if($num_rows == 0){
      echo "Database table empty";
      return;
  }
  /* Display table contents */
        echo "<label>Category:<br />\n";
        echo "<select>";
  for($i=0; $i<$num_rows; $i++){
 
      $rows = mysql_fetch_array($result);
       
        echo "<option value=".$rows[0].">".$rows[1]."</option>\n";
       
            }
        echo "</select>\n";   
        echo "</lable><br><br>\n";
       
        // list subcatories in the forum

        // echo "<label>Subcatory<br />\n";
        // echo "<option value='$id'>$name</option>";
        // echo "</lable>";

}
?>
[/code]

this is what I have so fare, and this function is called on another page, where the rest of the form is located.

currently this prints out the categories in a pulldown menu.

$row[0] = id value in the database for category table
$row[1] = name value in the database for category table
$row[2] = id value in the database for subcategory table
$row[3] = name value in the database for subcategory table

im pretty much stuck here, I can populate the pulldown menu for category but not sure how to do the subcategory and also change the values when the user selects a different category , and repopulate the subcategory based upon changes.

im assuming this needs a bit of javascript.

Any advice on how I should proceed.?

steve
Yes, to change the contents of a drop down menu without reloading the page, you would have to use javascript.  You could populate the drop down by indenting each subcategory....

Category 1
-->subcategory 1
-->subcategory 2
-->subcategory 3
Category 2
-->subcategory 1
-->etc......

That way you would only have to populate the drop down once, and you wouldn't have to worry about using any js.
never mind on the indent, i hadnt had my coffee yet, anyone point me to some way to work with PHP and JS for pulldown menus, I would like to expand my skills a bit and im sure Ill have to use this method at some point.

Steve
[url=http://ibareitall.com]I Bare It All Blog[/url]

Archived

This topic is now archived and is closed to further replies.

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