NikkiW
NikkiW

Reputation: 43

PHP and tags in a DB

I'm submitting tags into a DB like tag1,tag2,tag3. How would I go about splitting them so i could pharse them into links and then query the DB for other submissions that use those tags?

Upvotes: 3

Views: 101

Answers (3)

Geoffrey Wagner
Geoffrey Wagner

Reputation: 818

A very simple but very hackey approach would be to enter your tags with beginning and trailing commas so ,tag1,tag2,tag3,tag4, because then you can get tag2 by querying for where tag LIKE '%,tag2,%' Granted this is slow and there are way more advanced ways to do this via 1:many relationships. If you want to make tags into links I would look into a structure where 1 table has posts and one table has tags. http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html has a few table structures and explanations on how this works.

Upvotes: 0

Simon marc
Simon marc

Reputation: 1003

I'm not very sure to understand but if you want to parse your tag string you can use:

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

You'll normally get the a array with the 3 tag inside.

But you should insert tag one by one in another table a make reference on them.

Upvotes: 1

dqhendricks
dqhendricks

Reputation: 19251

You should probably keep tags in a separate table, and link them to the record using a field containing the record's id.

Most one to many relationships within a database call for separate tables. Comma separated values go against what makes databases so great.

Upvotes: 4

Related Questions