如何使用 PHP 和 MySQL 根据输入 MySQL 数据库的次数显示更大的文本?

发布于 2024-08-13 07:23:40 字数 1819 浏览 8 评论 0原文

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

我希望我解释得对吗?

这是 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫思念不要吵 2024-08-20 07:23:40

您输入标签 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'];
}

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'];
}
这个俗人 2024-08-20 07:23:40

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

questions_tags.users_questions_id=3

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

questions_tags.users_questions_id=3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文