爆炸在我的索引文件,我得到使用此功能(這是在另一個文件中)數據庫中的數據:標記和在PHP
function get_all_threads($link, $start)
{
$threads = select_Query("SELECT thread.title, thread.id as t_id,
thread.content, author.username, author.id as a_id,
GROUP_CONCAT(DISTINCT tags.name ORDER BY tags.name DESC SEPARATOR ',') AS tags
FROM thread JOIN thread_tags ON thread.id = thread_tags.thread_id
JOIN tags ON thread_tags.tag_id = tags.id
INNER JOIN author on author.id = thread.author_id
GROUP BY thread.id DESC
LIMIT $start, 30", $link);
return $threads;
}
在我的索引文件的代碼如下:
$threads = get_all_threads($link, $start);
include FORUM_ROOT . 'html/main/threads.html.php';
在threads.html.php
<?php foreach ($threads as $thread): ?>
<h1><?php echo $thread['title']; ?></h1>
<?php echo $thread['content']; ?>
<?php echo $thread['tags']; ?> <!-- All of these appear as one -->
<?php endforeach; ?>
正如你可以看到我得到的線程,它們的作者和他們的標籤。不過我使用GROUP_CONCAT的標籤,因此,當我要讓每一個人標籤的鏈接,如在threads.html.php:
<a href="?tag=<?php echo $thread['tags']; ?>" >
<?php echo $thread['tags']; ?>
</a>
所有標籤顯示爲一個鏈接,(例如,如果標籤是蘋果,橙,香蕉,他們將顯示爲apple, orange, banana,而不是apple,orange,banana):
我知道分手了這些標籤,我可以使用爆炸,然後遍歷每個如下:
<?php $tags = array();
$tags = explode(',', $thread['tags']);
?>
(Tagged:
<?php foreach ($tags as $tag): ?>
<a href="/tag?=<?php echo $tag;?>"><?php echo $tag; ?></a>
<?php endforeach; ?>)
不過我將 那麼必須在threads.html.php中做到這一點,我想保持演示代碼分離。
在index.php中有這樣做的方法嗎?同時遍歷每個線程和爆炸的每個標籤將花費太長的時間對於那些超過30
編輯主題:
我的表結構如下:
線程,author_threads,作家,標籤,thread_tags,回覆和author_replies
1:http://apple,橙,香蕉
可以添加你的數據庫表結構 – 2011-02-06 17:45:17
我想如果可能更喜歡問題簡單化......。 – Kissaki 2011-02-06 17:53:30
我不認爲`GROUP_CONCAT`是SQL標準......您使用哪種DBS? – Kissaki 2011-02-06 17:56:12