views:

49

answers:

2

Okay, I have this script that should display the tags that are entered more times bigger and tags that are entered less smaller for a certain question. But for some reason it displays the last tag entered bigger and displays all the tags that where entered before it smaller like if it was counting down. I need to fix this problem.

I hope I explained this right?

Here is the MySQL tables.

CREATE TABLE questions_tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag_id INT UNSIGNED NOT NULL,
users_questions_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

Here is my PHP script.

<?php

$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "sitename";

mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db($db_name);

function tag_info() {
$result = mysql_query("SELECT questions_tags.*, tags.* FROM questions_tags INNER JOIN tags ON tags.id = questions_tags.tag_id WHERE questions_tags.users_questions_id=3 ORDER BY users_questions_id DESC");
while($row = mysql_fetch_array($result)) {
$arr[$row['tag']] = $row['id'];
}
ksort($arr);
return $arr;
}

function tag_cloud() {

$min_size = 10;
$max_size = 30;

$tags = tag_info();

$minimum_count = min(array_values($tags));
$maximum_count = max(array_values($tags));
$spread = $maximum_count - $minimum_count;

if($spread == 0) {
$spread = 1;
}

$cloud_html = '';
$cloud_tags = array();

foreach ($tags as $tag => $count) {
$size = $min_size + ($count - $minimum_count)
* ($max_size - $min_size) / $spread;
$cloud_tags[] = '<a style="font-size: '. floor($size) . 'px'
. '" class="tag_cloud" href="http://www.example.com/tags/' . $tag .'/'
. '" title="\'' . $tag . '\' returned a count of ' . $count . '">'
. htmlspecialchars(stripslashes($tag)) . '</a>';
}
$cloud_html = join("\n", $cloud_tags) . "\n";
return $cloud_html;

}

?>

<div id="wrapper">
<?php print tag_cloud(); ?>
</div>
A: 

Perhaps the problem is the hardcoded id in the WHERE clause?

questions_tags.users_questions_id=3
ctford
This is to call the question web page id and should not really matter as it will be changed later on to a dynamic value.
myhut
I'm testing this on a single page to sum it up.
myhut
+1  A: 

Your entering the tag id as the value in the array thats why your tags get incrementally smaller.

Grouping by tag.id and running a count should fix your query.

$result = mysql_query("SELECT tag, count(*) as tagcount, tags.id FROM tags, questions_tags WHERE questions_tags.tag_id = tags.id AND questions_tags.users_questions_id=3 GROUP BY tags.id");

and you just assign the tagcount as your array value

while($row = mysql_fetch_array($result)) {
$arr[$row['tag']] = $row['tagcount'];
}
bmac