Answer the question
In order to leave comments, you need to log in
How to properly design this database?
For example, I have an entity "Writers (name, date of birth, date of death)". Writers are divided into domestic and foreign. Accordingly, the set of attributes for domestic and foreign will be different. Thus, I create 2 more tables "Domestic (writer id, etc. attributes unique to this type)" and also "Foreign" (writer id, etc. attributes unique to this type, such as language). If a third category of writers appears, then I will create another table in which I will describe the attributes unique to this type.
If you need to show a list of categories of writers - then I will create another table "Categories of writers" (id, category name), and in the tables "foreign", "domestic" and so on, I will add the field "writer_category_id" - which will refer to the name of the category. Now there is a list of categories, which I can already show.
The main question - When the user starts filtering writers by categories - then in the code I have to write through switch , that if, for example, the category id is 2, then I use the foreign table, if there is another code, then another table. Is this correct - wouldn't it be too much code? Can it be handled differently? After all, there can be, for example, 15 categories, and then you have to write 15 branches for switch? (Even if it's a factory, you still have to use switch)
The most radical, and, in my opinion, wrong, is to add the table_name attribute to the "Writers Categories" table.
Answer the question
In order to leave comments, you need to log in
In general, there is a well-established scheme for such a task:
categories (id, name) - categories (domestic, foreign, etc.)
items (id, category_id, basic set of attributes) - in your case, the authors
attributes (id, name, type, ...) - possible attributes
categories_attributes (category_id, attribute_id) - attributes allowed for the category
items_attributes (item_id, attribute_id, value) - attribute values themselves
This scheme is easily extended, may contain additional information - attribute mandatory attribute in the description, list of valid values attributes for types "one from a list", "multiple from a list", units of measure, etc.
Bind it all with foreign keys , and instead of switch use:
$category_id = htmlspecialchars($_GET['category_id']);
$sql = $pdo->query('SELECT * FROM table_name WHERE `writer_category_id` = :category_id', ['category_id' => $category_id]);
$results = $sql->fetch();
Where did you learn so well? Never heard of normal forms, I take it?
Of course, and more if needed! More tables for the table god! )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question