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? Link to comment https://forums.phpfreaks.com/topic/167261-solved-too-many-tables/ 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 Link to comment https://forums.phpfreaks.com/topic/167261-solved-too-many-tables/#findComment-881900 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' Link to comment https://forums.phpfreaks.com/topic/167261-solved-too-many-tables/#findComment-882335 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. Link to comment https://forums.phpfreaks.com/topic/167261-solved-too-many-tables/#findComment-882410 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. Link to comment https://forums.phpfreaks.com/topic/167261-solved-too-many-tables/#findComment-882559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.