Answer the question
In order to leave comments, you need to log in
How to organize the output from the database of data specified separated by commas?
Good day.
I add categories to the database by specifying their IDs separated by commas (for example, column category = 1,3,4), there is a separate category table, where these numbers are field IDs. Since the file (categories for the file are made) can consist of several categories (from 1 to infinity), I see no reason to create columns for a separate category. There were no problems with the output on the file page (explode and you're done). But then I ran into this problem. On the page of the category itself, where files are displayed, I cannot organize the search and output of files if the file has several categories.
$cid = $_GET['id']; // допустим, что это ID той категории, что я сейчас просматриваю.
$stmt = $go -> prepare("SELECT `id` FROM `images` WHERE `category` = ? ORDER BY `id` DESC LIMIT ?, ?");
$stmt -> execute([$cid, $nav -> start(), $max]);
$images = $stmt -> fetchAll();
Answer the question
In order to leave comments, you need to log in
I would make a separate table, for example, images_to_category, and it has 2 columns: image_id, category_id, and I would write each image connection with the category separately in a row, i.e. if let's say the image file.jpg has id 3 and is in 3 categories with id 7, 20 and 45 then this table would have entries:
3 | 7
3 | 20
3 | 45
and when selecting by the desired category, I would use JOIN. For example, if I view a category with id 7, then the query should go to the database like this:
SELECT i.id FROM images i
JOIN images_to_category i2c ON i.id=i2c.image_id
WHERE i2c = 7;
faced such problem
That's why I asked such a question, because I simply don't know how else I can implement it so that the file is in several categories at the same time and is easily searched for in the database.You have a problem with the DBMS. It does not support list operations: STRING_SPLIT (Transact-SQL) .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question