PHP and tags in the database

I send tags to DB such as tag1, tag2, tag3. How would I go about separating them so that I can direct them to links and then query the DB for other views that use these tags?

+3
source share
3 answers

You should probably store the tags in a separate table and associate them with the record using a field containing the record identifier.

From one to many relationships inside a database call for individual tables. Values ​​separated by commas contradict what makes databases so large.

+4
source

I don't really understand it, but if you want to parse a tag string, you can use:

<?php 
  str=" tag1, tag2, tag3 "; 
  $array = explode(', ', trim(str)); 
?>

Usually you will get an array with tag 3.

, .

+1

A very simple but very hacky approach is to enter your tags with start and end commas, therefore ,tag1,tag2,tag3,tag4,, because then you can get tag2 by querying for where tag LIKE '%,tag2,%'. Granted, this is slow, and there is a way to more advanced ways to do it through 1: many relationships. If you want to make tags in links, I would consider a structure in which 1 table has messages and one table has tags. http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html contains several table structures and explains how this works.

0
source

Source: https://habr.com/ru/post/1785268/


All Articles