I have a post system where the user makes a post with the following fields: title, description, tags (comma separated) how could I make a table record through the tags and display the related postings?
I have a post system where the user makes a post with the following fields: title, description, tags (comma separated) how could I make a table record through the tags and display the related postings?
1st case: You can do something like this:
SELECT *.noticias,
noticias.tags
FROM noticias
WHERE noticias.tags IN('mysql', 'php', 'jquery')
ORDER BY data_noticias DESC, noticias.tags;
I can clarify:
Select everything from the news table and the value of the tags field of the news table where the value of the tags field is in the list (value 1, value 2, value 3), and sort by descending date and by tags ascending.
2nd case: You can do this in a relational way too, with three tables, you register the tags within a table and the relationship between tables in another table, relating everything to the foreign key. Let's say you have three tables:
1st table - news:
+----------------------------------------------+
| id_noticia | titulo | descricao |
+----------------------------------------------+
| 1 | exemplo 1 | lorem inpsun dolor |
+----------------------------------------------+
| 2 | exemplo 2 | lorem inpsun dolor |
+----------------------------------------------+
2nd table - table_tags: Create a table to register all your tags
+----------------+
| id_tag | tag |
+----------------+
| 1 | tag1 |
+----------------+
| 2 | tag2 |
+----------------+
| 3 | tag3 |
+----------------+
3rd table - table_rel_tags: "id_fk_noticia" would be the "news_id" (from the news table) and "id_fk_tag" would be the id_tag of the table_tags table.
+--------------------------------------+
| id_rel | id_fk_tag | id_fk_noticia |
+--------------------------------------+
| 1 | 1 | 1 |
+--------------------------------------+
| 2 | 2 | 2 |
+--------------------------------------+
| 3 | 3 | 2 |
+--------------------------------------+
So, you collect the tags from the relational table, Example:
//declara o tipo: array
$select= array();
//traz do banco as tags consultadas
$tags = array(
1 => 'tag1',
2 => 'tag2',
3 => 'tag3'
);
//inicia o array de seleções
$select[] = "SELECT *.noticias";
//faz o loop nas tags consultadas
foreach ($tags as $id_tag => $tag) {
//na query você verifica se a id da tabela tag consta na sua tabela relacional, se existir você cria um alias com o nome da tag e traz a id no campo
$select[] = "IF(tabela_rel_tags.tags = '$id_tag', tabela_rel_tags.tags, null) as '$tag'";
}
//monta a string da sua query separando-a por vírgulas
$sql = implode(", ", $select);
//concatena o resto da sua query que irá relacionar as tabelas
$sql .= "
FROM noticias
INNER JOIN tabela_rel_tags ON(tabela_rel_tags.id_fk_noticias = noticias.id_noticia)
INNER JOIN tabela_tags ON (tabela_tags.id_tag = tabela_rel_tags.id_tag)
WHERE tabela_tags.tags IN (".implode(",", $tags).")
ORDER BY data_noticias DESC, noticias.tags; ";
SELECT *.posts,
posts.tags
FROM posts
WHERE posts.tags IN('mysql', 'php', 'jquery')
ORDER BY date_post DESC, posts.tags;
select*.posts
: would be the table of your posts and when it appears posts.tags would relate the video and the tags.