PHP 實作知識問答網站中的標籤雲和話題聚合功能
在知識問答網站中,標籤雲和話題聚合是兩個重要的功能。標籤雲可以幫助使用者快速了解網站上的熱門話題和常見標籤,方便使用者瀏覽和搜尋相關的問題和答案。而話題聚合則可以將相同標籤的問題和答案集中在一起,提供使用者更方便的檢視和討論方式。下面我們將使用 PHP 來實作這兩個功能。
首先,我們需要建立一個資料庫來儲存問題、答案和標籤的資料。可以建立一個名為 "qa" 的資料庫,其中包含三個表:questions、answers 和 tags。
questions 表的結構如下:
1 2 3 4 5 6 7 | CREATE TABLE `questions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT '' ,
`content` text,
` date ` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
登入後複製
answers 表的結構如下:
1 2 3 4 5 6 7 | CREATE TABLE `answers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question_id` int(11) DEFAULT NULL,
`content` text,
` date ` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
登入後複製
tags 表的結構如下:
1 2 3 4 5 | CREATE TABLE `tags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT '' ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
登入後複製
接下來,我們需要在知識問答網站的提問頁面和回答頁面上新增標籤選擇框,並將使用者選擇的標籤與問題或答案關聯起來。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <!-- 提问页面 -->
<form action= "post_question.php" method= "post" >
<label for = "title" >问题标题:</label>
<input type= "text" name= "title" id= "title" ><br>
<label for = "content" >问题内容:</label>
<textarea name= "content" id= "content" ></textarea><br>
<label for = "tags" >标签:</label>
<select name= "tags[]" id= "tags" multiple>
<option value= "php" >PHP</option>
<option value= "javascript" >JavaScript</option>
<option value= "html" >HTML</option>
<!-- 其他标签选项 -->
</select><br>
<input type= "submit" value= "提交问题" >
</form>
<!-- 回答页面 -->
<form action= "post_answer.php" method= "post" >
<label for = "content" >回答内容:</label>
<textarea name= "content" id= "content" ></textarea><br>
<label for = "tags" >标签:</label>
<select name= "tags[]" id= "tags" multiple>
<option value= "php" >PHP</option>
<option value= "javascript" >JavaScript</option>
<option value= "html" >HTML</option>
<!-- 其他标签选项 -->
</select><br>
<input type= "submit" value= "提交回答" >
</form>
|
登入後複製
然後,我們需要在背景的問題提交處理程序(post_question.php)和回答提交處理程序(post_answer.php)中,將問題或答案關聯的標籤資料存入資料庫。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | $title = $_POST [ 'title' ];
$content = $_POST [ 'content' ];
$tags = $_POST [ 'tags' ];
$question_id = mysqli_insert_id( $conn );
foreach ( $tags as $tag ) {
$sql = "INSERT INTO tags (title) VALUES ('$tag')" ;
mysqli_query( $conn , $sql );
$tag_id = mysqli_insert_id( $conn );
$sql = "INSERT INTO question_tag (question_id, tag_id) VALUES ($question_id, $tag_id)" ;
mysqli_query( $conn , $sql );
}
$content = $_POST [ 'content' ];
$tags = $_POST [ 'tags' ];
$answer_id = mysqli_insert_id( $conn );
foreach ( $tags as $tag ) {
$sql = "INSERT INTO tags (title) VALUES ('$tag')" ;
mysqli_query( $conn , $sql );
$tag_id = mysqli_insert_id( $conn );
$sql = "INSERT INTO answer_tag (answer_id, tag_id) VALUES ($answer_id, $tag_id)" ;
mysqli_query( $conn , $sql );
}
|
登入後複製
現在,我們已經成功將使用者選擇的標籤與問題和答案關聯起來了。接下來,我們將實作標籤雲和話題聚合的功能。
首先,我們需要寫一個 PHP 函數來取得所有標籤及其出現次數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function getTagsCloud() {
global $conn ;
$sql = "SELECT title, COUNT(*) AS count FROM tags GROUP BY title" ;
$result = mysqli_query( $conn , $sql );
$tagsCloud = array ();
while ( $row = mysqli_fetch_assoc( $result )) {
$tagsCloud [ $row [ 'title' ]] = $row [ 'count' ];
}
return $tagsCloud ;
}
|
登入後複製
然後,我們可以在前端頁面上使用該函數來展示標籤雲。
1 2 3 4 5 6 7 8 9 10 11 | <?php
$tagsCloud = getTagsCloud();
?>
<!-- 标签云 -->
<div class = "tag-cloud" >
<?php foreach ( $tagsCloud as $tag => $count ): ?>
<a href= "search.php?tag=<?php echo $tag; ?>" ><?php echo $tag ; ?></a>
<?php endforeach ; ?>
</div>
|
登入後複製
最後,我們可以實作話題聚合的功能,也就是根據標籤查詢相關的問題和答案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | function getQuestionsByTag( $tag ) {
global $conn ;
$sql = "SELECT q.id, q.title, q. date , COUNT (*) AS answersCount
FROM questions q
INNER JOIN question_tag qt ON q.id = qt.question_id
INNER JOIN tags t ON t.id = qt.tag_id
WHERE t.title = '$tag'
GROUP BY q.id";
$result = mysqli_query( $conn , $sql );
$questions = array ();
while ( $row = mysqli_fetch_assoc( $result )) {
$questions [] = $row ;
}
return $questions ;
}
function getAnswersByTag( $tag ) {
global $conn ;
$sql = "SELECT a.id, a.content, a. date , q.title
FROM answers a
INNER JOIN answer_tag at ON a.id = at.answer_id
INNER JOIN tags t ON t.id = at.tag_id
INNER JOIN questions q ON q.id = a.question_id
WHERE t.title = '$tag' ";
$result = mysqli_query( $conn , $sql );
$answers = array ();
while ( $row = mysqli_fetch_assoc( $result )) {
$answers [] = $row ;
}
return $answers ;
}
|
登入後複製
我們可以在主題詳情頁面上使用上述兩個函數來展示相關的問題和答案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php
$tag = $_GET [ 'tag' ];
$questions = getQuestionsByTag( $tag );
$answers = getAnswersByTag( $tag );
?>
<!-- 话题相关的问题 -->
<?php foreach ( $questions as $question ): ?>
<div class = "question" >
<h3><?php echo $question [ 'title' ]; ?></h3>
<p>回答数量: <?php echo $question [ 'answersCount' ]; ?></p>
<!-- 其他问题信息展示 -->
</div>
<?php endforeach ; ?>
<!-- 话题相关的回答 -->
<?php foreach ( $answers as $answer ): ?>
<div class = "answer" >
<h4><?php echo $answer [ 'title' ]; ?></h4>
<p><?php echo $answer [ 'content' ]; ?></p>
<!-- 其他回答信息展示 -->
</div>
<?php endforeach ; ?>
|
登入後複製
透過上述程式碼範例,我們成功實現了知識問答網站中的標籤雲和話題聚合功能,用戶可以輕鬆瀏覽熱門話題和相關的問題和答案。同時,我們也提供了對應的資料庫操作範例,讓讀者更能理解程式碼的實作思路。希望本文對有需要的讀者有幫助。
以上是PHP 實作知識問答網站中的標籤雲和話題聚合功能。的詳細內容。更多資訊請關注PHP中文網其他相關文章!