zhangy Posted August 1, 2009 Share Posted August 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/ Share on other sites More sharing options...
kickstart Posted August 1, 2009 Share Posted August 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-888337 Share on other sites More sharing options...
Lambneck Posted August 2, 2009 Share Posted August 2, 2009 How can I set up said table? Each blog user submits multiple and often unique tags with each of their posts! Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-888803 Share on other sites More sharing options...
kickstart Posted August 2, 2009 Share Posted August 2, 2009 Hi Just something like Id BlogId User TagDetails. Just insert one row for each tag. Can easily be joined back to the full blog to get the details when required. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-888837 Share on other sites More sharing options...
zhangy Posted August 2, 2009 Author Share Posted August 2, 2009 so on average people post 3 tag words/phrase with each post are you saying each tag word/phrase should have its own row? (if so i have no idea how to do that) Or just take all the tags from one post and put them in a table row? Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-888880 Share on other sites More sharing options...
kickstart Posted August 2, 2009 Share Posted August 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-888894 Share on other sites More sharing options...
zhangy Posted August 3, 2009 Author Share Posted August 3, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-889198 Share on other sites More sharing options...
roopurt18 Posted August 3, 2009 Share Posted August 3, 2009 <?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! ?> Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-889278 Share on other sites More sharing options...
zhangy Posted August 3, 2009 Author Share Posted August 3, 2009 this may be a dumb question but... you entered the value of $tags manually. $tags = 'tag1, tag2, tag3, ..., tagN'; How to get the tags a users entered via the html form as the value of $tags? Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-889482 Share on other sites More sharing options...
kickstart Posted August 3, 2009 Share Posted August 3, 2009 Hi What does your form look like that they have entered the tags on? Is there one input field for all the tags they add? One input field per tag? All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-889518 Share on other sites More sharing options...
roopurt18 Posted August 3, 2009 Share Posted August 3, 2009 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']. Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-889780 Share on other sites More sharing options...
zhangy Posted August 4, 2009 Author Share Posted August 4, 2009 oic. ok now i understanad. thank you both very much!! Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-890443 Share on other sites More sharing options...
zhangy Posted August 4, 2009 Author Share Posted August 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-890473 Share on other sites More sharing options...
kickstart Posted August 4, 2009 Share Posted August 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-890483 Share on other sites More sharing options...
zhangy Posted August 4, 2009 Author Share Posted August 4, 2009 Like this?: $insert = mysql_query("INSERT INTO $tags_table (blog_id, user, tag) VALUES (".mysql_insert_id().", '$name', ". implode( ', ', $tags ).")"); Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-890493 Share on other sites More sharing options...
kickstart Posted August 4, 2009 Share Posted August 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168399-best-practise-setting-up-a-database-for-tags/#findComment-890506 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.