1

好的,我有这个脚本应该显示对于某个问题输入的标签要大几倍,而输入的标签要小一些。但由于某种原因,它显示最后输入的标签更大,并显示在它之前输入的所有标签,就像它正在倒计时一样。我需要解决这个问题。

我希望我解释得对吗?

这是 MySQL 表。

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)
);

这是我的 PHP 脚本。

<?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>
4

2 回答 2

1

您输入标签 ID 作为数组中的值,这就是标签逐渐变小的原因。

按 tag.id 分组并运行计数应该可以修复您的查询。

$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");

并且您只需将 tagcount 分配为您的数组值

while($row = mysql_fetch_array($result)) {
$arr[$row['tag']] = $row['tagcount'];
}
于 2009-12-04T19:23:19.747 回答
0

也许问题是 WHERE 子句中的硬编码 id?

questions_tags.users_questions_id=3
于 2009-12-04T18:17:56.183 回答