How I Added a Weighting to My Tags

In an extension to How I Added Tagging Using PHP and MySQL, I'll cover how I added a weighted tag list to FONTSMACK. Again, the process is fairly straightforward except with a little math this time.

There are essentially two steps to this process.

Step 1: Determine the number of steps

while($ftypes = mysql_fetch_array($rs))
{
  $types = array_merge($types, explode(" ", trim($ftypes[0]) ));
}

$typecount = array_count_values($types);
$types = array_unique_compact($types);

$max = max($typecount);
$min = min($typecount);

$x = 18; // 18px
$y = 11; // 11px

$stepvalue = ($max - $min) / ($x - $y);	

The while statement and the use of array_unique_compact are the same as before. array_count_values tallies up the number of each tag before removing the duplicates. max returns the the largest value contained within the array and min appropriately returns the smallest value.

In this case, I've determined that the maximum font size should be 18px and the minimum font size should be 11px. The step value is then determined by taking the difference between max and min counts and dividing them by the difference between the max and min font sizes.

If I had 20 fonts for one tag, only 1 for another, and I want 7 different font sizes in between then there my step value would be 3 (I'm rounding here... the actual step value would be just under 3 with a bunch of decimal points).

Step 2: Apply the number of steps

for($i=0;$i<count($types);$i++)
{
  echo '<a href="/fonts?t='.$types[$i].'" 
  style="font-size:'. ( $y + round( ($typecount[$types[$i]]-$min) / $stepvalue ) ).'px;">'. $types[$i].'</a>';
  if($i<count($types)-1) echo ",\n";
}

As I output each font to the page, I use this basic formula to calculate the font size: number of fonts for this tag minus minimum number of fonts divided by the step value and then add that to the base font size.

Using my previous example, if this particular tag had 12 fonts then the font size would be my base font of 11px plus 4px for a total of 15px.

Published July 25, 2005 · Updated September 14, 2006
Categorized as PHP
Short URL: https://snook.ca/s/389

Conversation

18 Comments · RSS feed
Wesley Walser said on July 29, 2005

Noticed the other day that the tags were weighted, and though to myself 'hrmm, he hasn't said anything about that'.

As usual it's great to see how someone acomplished a task in a real application.

Bassi Federico said on July 29, 2005

Thanx for the code :-) the $stepvalue variable could be even 0, so the division would produces an "error: division by zero". To avoit the error put an if(0 == $stepvalue) $stepvalue = 1;

IsmaSan said on August 01, 2005

or
$stepvalue = !$stepvalue?1:$stepvalue;

Couldn't your approach of storing ALL tags in a php array get too heavy with massive amounts of tags? Same with using LIKE in your SQL...

Great site and fontsmack rocks!

Gaurav said on November 26, 2005

Gr8 site and tutorial.

You can also make the list as Alphabetical Weighted List. Just need to add one line.

[....]
$typecount = array_count_values($types);
$types = array_unique_compact($types);

$types=sort($types);
[...]

said on February 17, 2006

this is awesoemf

adam said on February 28, 2006

you are the best web site in the worild

Richard said on May 31, 2006

THANK YOU THANK YOU THANK YOU!
I was about to throw my computer out the window!

Aamir Mahmood said on September 06, 2006

I always like your code, i added your tags code in a client's website, and now he asked me to do same like blogmarks.net and i was searching and searching and came again to your post and it worked

Thank Buddy

jack said on January 05, 2007

This is cool! I am going to implement it on my site real soon.

Jack

amin said on January 09, 2007

Interesting...but i'm using PHP4.3 and couldn't get the function name array_unique_compact.....I'm newbie in PHP. Could suggest any action can i take for this problem..

Marc said on February 01, 2007

good solution. i realised my tagcloud in a similar way:

http://www.onlinestreet.de/tags/

amin2u said on February 04, 2007

ok now i'm done with this weighting tags...i'm having a problem where i want to pull all the related tag. Let's say user pick a tag named malaysia. There are also related tag such as kuala lumpur, kedah, melaka, and so on. I hope that someone could give me the idea on how to do this

Jack said on March 31, 2007

I really like this idea, really quite original!

Gabe said on May 25, 2007

Are you pulling all the tags out of the mySQL table? You really should use "GROUP BY" and "COUNT()" so that you don't pull duplicate tags from the mySQL table as well as weigh the tags.

Manuel said on July 30, 2007

Cool, I will try to get this working on my site.

J said on September 09, 2007

//Weight for tag re-occurance
natsort($tagcount);
$tagcount = array_reverse($tagcount);
$tags = array_keys($tagcount);

=)

Mac Millan said on September 10, 2007

couldn't get the function name array_unique_compact either, any help?

Jonathan Dingman said on September 12, 2007

Wow, Jonathan, you've done a great job with this. It is a very straight forward tutorial and is easy to use, thank you.

Definitely bookmarking this site for future updates, you are a great writer.

thanks again so much Jonathan.

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.