c-o-d-e Posted December 11, 2009 Share Posted December 11, 2009 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. Link to comment https://forums.phpfreaks.com/topic/184791-categories-and-sub-categories-in-a-database-help/ Share on other sites More sharing options...
nozai Posted December 11, 2009 Share Posted December 11, 2009 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... Link to comment https://forums.phpfreaks.com/topic/184791-categories-and-sub-categories-in-a-database-help/#findComment-975543 Share on other sites More sharing options...
c-o-d-e Posted December 11, 2009 Author Share Posted December 11, 2009 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. Link to comment https://forums.phpfreaks.com/topic/184791-categories-and-sub-categories-in-a-database-help/#findComment-975571 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.