2009-12-04 105 views
1

好的,我有這個腳本,它應該顯示輸入次數更多的標籤,以及對於某個問題輸入的標籤更小。但由於某種原因,它顯示輸入的最後一個標籤更大,並顯示所有標籤之前輸入的標籤更小,就像它正在倒計時一樣。我需要解決這個問題。如何根據使用PHP和MySQL輸入到MySQL數據庫的次數來顯示更大的文本?

我希望我解釋過這個嗎?

這裏是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> 
+0

@myhut你解決你的問題了嗎? @ bmac的答案應該適合你。 – 2009-12-05 02:26:56

回答

0

也許問題是在WHERE子句中的硬編碼ID?

questions_tags.users_questions_id=3 
+0

這是調用問題的網頁ID,應該沒有關係,因爲它稍後會更改爲動態值。 – myhut 2009-12-04 18:20:20

+0

我在單個頁面上測試這個總結。 – myhut 2009-12-04 18:20:54

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']; 
} 
相關問題