asmon Posted July 24, 2009 Share Posted July 24, 2009 i have a table of categories and a table of subjects. a subject can be related to more than one category therefore i created a 3rd table: category_subject with the relationship between them. then to select it, i'm doing something like this static function select_subjects_by_cat_id($selection, $current_id) { global $db; $new_arr = array(); $query = $db->sql_query("select $selection from $db->subjects_table join $db->subject_category on $db->subject_category.subj_id = $db->subjects_table.id where $db->subject_category.cat_id = $current_id"); while ($result = $db->sql_fetch_array($query)) { $new_arr[] = $result; } return $new_arr; } it works great but then i have to relate many users to one subject. so again, i have to make another table: user_subjects. my site has many relations therefore i'm ending up with many tables is it fine doing it this way or there's a better solution? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 24, 2009 Share Posted July 24, 2009 Don't fret, you are doing it right Quote Link to comment Share on other sites More sharing options...
waynew Posted July 24, 2009 Share Posted July 24, 2009 I don't see why a third table is needed? Somebody set me straight if I'm wrong, but wouldn't it be better to do it this way: Category: category_id category_name Subject: subject_id subject_name category_id So, to get all subjects from a specific category, just SELECT * FROM Subject WHERE category_id = '$cat_id' Quote Link to comment Share on other sites More sharing options...
methomps Posted July 25, 2009 Share Posted July 25, 2009 Because a subject can belong to more than one category. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 25, 2009 Share Posted July 25, 2009 My bad, I completely misunderstood (was tired) the entire post. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.