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.
Conversation
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.
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;
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!
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);
[...]
this is awesoemf
you are the best web site in the worild
THANK YOU THANK YOU THANK YOU!
I was about to throw my computer out the window!
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
This is cool! I am going to implement it on my site real soon.
Jack
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..
good solution. i realised my tagcloud in a similar way:
http://www.onlinestreet.de/tags/
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
I really like this idea, really quite original!
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.
Cool, I will try to get this working on my site.
//Weight for tag re-occurance
natsort($tagcount);
$tagcount = array_reverse($tagcount);
$tags = array_keys($tagcount);
=)
couldn't get the function name array_unique_compact either, any help?
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.