Jump to content

best practise setting up a database for tags


zhangy

Recommended Posts

Hello,

I am wondering whats the best way to store tags from blog posts in a database.

Just stuff tags from each post in a row in the same table as the actual post?

Create a table entirely dedicated to tags? or what?

Ive never done this before. so any help will be appreciated.

thanks.

Link to comment
Share on other sites

Hi

 

Seperate table for tags, one row per tag.

 

If you put all the tags for one blog in one field then it makes it very difficult and inefficient to get the tag details in one SQL operation.

 

Keeping them in a seperate table makes joins to related data far easier.

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Each tag should have its own row. Major point of a relational database like MySQL is that you can join related rows, something that is very difficult to do if you just lob all the keys together into one field.

 

(if so i have no idea how to do that)

 

You just do an insert rather than updating the row for the post. Without more details of the script you have then not much more I can say.

 

All the best

 

Keith

Link to comment
Share on other sites

well currently each posts tags are being stored in the same row in the same table as the rest of the blog post data. really the part I dont understand is how to seperate the tags from each other.

so for example if the tags of a blog post are submitted like this:

tag1, tag 2, tag3

How to seperate them and store them into individual rows?

Link to comment
Share on other sites

<?php
$tags = 'tag1, tag2, tag3, ..., tagN';
$tags = explode( ',', $tags );
foreach( $tags as $k => $v ) {
  $v = "'" . mysql_real_escape_string( trim( $v ) ) . "'";
  $tags[$k] = "( {$v} )";
}
$insert = "insert into `tags_table` ( `tag` ) values " . implode( ', ', $tags );
echo $insert;
// You should know what to do from here!
?>

Link to comment
Share on other sites

That depends on your form.

 

You told us they were submitted like tag1, tag2, tag3 so I showed you how to split and insert them if there is a variable holding a string like that.

 

Just replace my $tags = with something like $tags = $_POST['tags'].

Link to comment
Share on other sites

sorry I forgot to ask something. How to connect the tags stored in the tags table with the rest of the blog data stored in the other table? my  tags table looks like what kickstart suggested:

 

Id

BlogId

User

TagDetails.

 

I'm guessing the way to connect them would be to get the Id of the blog post in the BlogTable and insert it into the "BlogId" in the TagsTable. But how?

Link to comment
Share on other sites

I'm guessing the way to connect them would be to get the Id of the blog post in the BlogTable and insert it into the "BlogId" in the TagsTable. But how?

 

Yes, that is what you would do. I would presume that you have the id o the blog post as a field on the form (probably hidden), as you would need it when you update it.

 

If you are inserting a fresh blog post and various tags at the same time then you would insert the blog and gets it id by using mysql_insert_id() and the insert the various tags.

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Afraid not, as that would just insert a single line (which would probably fail as the number of columns wouldn't match).

 

You would probably need something like this. Insert a record onto the blog table and then loop round inserting tags.

 

<?php
$insert = mysql_query("insert into `blogs_table` ( `blog` ) values ('$blog')");
$BlogId = mysql_insert_id();
$tags = 'tag1, tag2, tag3, ..., tagN';
$tags = explode( ',', $tags );
$name = 'Some Bloggers Name';
foreach( $tags as $k => $v ) 
{
$v = "'" . mysql_real_escape_string( trim( $v ) ) . "'";
$insert = mysql_query("INSERT INTO `tags_table` (blog_id, user, tag) VALUES ($BlogId, '$name', '$v')");
}
// You should know what to do from here!
?>

 

You could also do this which is a variation on the idea from zhangy, but not sure at this level it would be harder for you to follow and maintain. It should be far more efficient though.

 

<?php
$insert = mysql_query("insert into `blogs_table` ( `blog` ) values ('$blog')");
$BlogId = mysql_insert_id();
$tags = 'tag1, tag2, tag3, ..., tagN';
$tags = explode( ',', $tags );
$name = 'Some Bloggers Name';
foreach( $tags as $k => $v ) {
  $v = "'" . mysql_real_escape_string( trim( $v ) ) . "'";
  $tags[$k] = "($BlogId, '$name',  '$v' )";
}
$insert = mysql_query("insert into `tags_table` ( `tag` ) values " . implode( ', ', $tags ));
// You should know what to do from here!
?>

 

Excuse any typos.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.