Jump to content

Categories and sub categories in a database help


c-o-d-e

Recommended Posts

I have two tables.

Categories

Subcategories

 

A user submits a category, or a sub category if necessary.

Certain Subcategories are different depending on each category.

 

The only way I could do it, is so all subcategories are the same for each category.

 

What is the best way to do it? I can't think.

I'm not too sure if I understand your question, but it sounds like you need help linking the data from those 2 tables.  You could have:

 

categories:
     category_id INT
     name VARCHAR/TEXT

subcategories:
     subcategory_id INT
     category_id INT
     name VARCHAR/TEXT

 

Then, you can JOIN them together to get the data you need...

I understand.

 

   $q = mysql_query("SELECT Category FROM Categories ORDER BY Category ASC") or trigger_error('Query failed: '. mysql_error());

    echo 'Category<br />'.$error['category'].'<select name="category" id="category">';

    while($row = mysql_fetch_array($q))
    {
    echo '<option>'. $row['Category'] .'</option>';
    }
    echo '</select><br />';

    $q = mysql_query("SELECT Subcategory FROM Subcategories WHERE Category = '$category' ORDER BY Subcategory ASC") or trigger_error('Query failed: '. mysql_error());

echo 'Sub Category<br />'.$error['subcategory'].'<select name="subcategory" id="subcategory">';

    while($row = mysql_fetch_array($q))
    {
    echo '<option>'. $row['Subcategory'] .'</option>';
    }
  
    echo '</select><br />';

 

The second query, is suppose to get the value of the menu list before when a user chooses. Then selects the sub categories depending on what they choose.

At the moment, I have $category

Though $category isn't defined.

I was hoping to define $category as $_POST['category'] but that would not work until after a user submits.

Do you understand? what's the best way to do it. It might need a while but I'm not sure at all.. or how to do it.

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.