Jump to content

Tag column in mysql and tags cloud


etrader

Recommended Posts

I have a tag column in the table of posts. I store tags as "tag1,tag2,tag3,...". When displaying tags in the post, I explode to have an array. Everything is OK, but I cannot create a tags cloud; as I need to have the occurrence of each tag to increase the font. What is a practical trick to create tag clouds?

 

I think the common method is to build a table for post tags and store each single tag in a separate cell. But I hope to keep my database tidy.

Link to comment
https://forums.phpfreaks.com/topic/245706-tag-column-in-mysql-and-tags-cloud/
Share on other sites

you could just do a random thing: (untested, off the top of my head)

 

$words = explode(",",$var); // assume this array holds your words
$sizes = array(10,13,16); // font sizes you want
foreach($words as $word){
   $size = rand(0,sizeof($sizes)); // randomly choose a size
   echo '<font size="'.$sizes[$size].'">'.$word.'</font>';
}

I think the common method is to build a table for post tags and store each single tag in a separate cell. But I hope to keep my database tidy.

 

Using a separate table is the proper way to do it. It does not make the database un-tidy; it makes it normalized. You are not going to find an efficient or accurate way to query the tags if they are stored in a delimited field that way. Once they are in a separate table, it is very simple to determine the number of posts associated with each tag. As it is, you will have to read every post in the database to get a tag count, and you will have to read every post in the database to find all posts associated with a specific tag. This will put an unnecessary load on the database. Having a normalized database will actually result in less of a load when looking for tag counts, or posts associated with a tag, or any other query against the tags.

I think the common method is to build a table for post tags and store each single tag in a separate cell. But I hope to keep my database tidy.

 

Using a separate table is the proper way to do it. It does not make the database un-tidy; it makes it normalized. You are not going to find an efficient or accurate way to query the tags if they are stored in a delimited field that way. Once they are in a separate table, it is very simple to determine the number of posts associated with each tag. As it is, you will have to read every post in the database to get a tag count, and you will have to read every post in the database to find all posts associated with a specific tag. This will put an unnecessary load on the database. Having a normalized database will actually result in less of a load when looking for tag counts, or posts associated with a tag, or any other query against the tags.

 

Yeah, what he ^^ said

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.