0
我的腳本應該從當前帖子類別中找到所有相關帖子,這些帖子可以是一個或多個類別,但我想停止腳本將當前帖子顯示爲相關帖子。我怎樣才能做到這一點?我在哪裏添加它到我的腳本?PHP和MySQL顯示問題
這是我的MySQL表。
CREATE TABLE categories (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
parent_id INT UNSIGNED NOT NULL DEFAULT 0,
category VARCHAR(255) NOT NULL,
url VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
INDEX parent (parent_id),
UNIQUE KEY(parent_id, url)
);
CREATE TABLE users_posts (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
title TEXT NOT NULL,
summary TEXT DEFAULT NULL,
post_content LONGTEXT NOT NULL,
PRIMARY KEY (id)
);
這是我的腳本原諒這個爛攤子。
$posts_categories = array();
$ac_query = mysqli_query($mysqli, "SELECT category_id
FROM posts_categories
WHERE post_id = '" . $post_id . "'");
if (!$ac_query) {
print mysqli_error($mysqli);
} else {
while($row = mysqli_fetch_array($ac_query)){
$posts_categories[] = $row['category_id'];
}
}
$posts_categories_name = array();
$acn_query = mysqli_query($mysqli, "SELECT category
FROM categories
WHERE id IN(" . implode(',', $posts_categories) . ")");
if (!$acn_query) {
print mysqli_error($mysqli);
} else {
while($row = mysqli_fetch_array($acn_query)){
$posts_categories_name[] = $row['category'];
}
}
$x2 = '';
$c2 = '';
foreach($posts_categories_name as $acn) {
$x2++;
if($x2 == 1){
$c2 .= " users_posts.title LIKE '%$acn%' OR users_posts.summary LIKE '%$acn%' OR users_posts.post_content LIKE '%$acn%'";
} else {
$c2 .= " OR users_posts.title LIKE '%$acn%' OR users_posts.summary LIKE '%$acn%' OR users_posts.post_content LIKE '%$acn%'";
}
}
$rac_query = mysqli_query($mysqli, "SELECT *
FROM users_posts
WHERE $c2
ORDER BY RAND()
LIMIT 5");
if (!$rac_query) {
print mysqli_error($mysqli);
} else {
while($row = mysqli_fetch_array($rac_query)){
echo '<li>' . $purifier->purify(strip_tags($row['title'])) .'</li>';
}
}
有沒有辦法檢查'$ current_post_id'是否是循環中的唯一結果? – slick 2010-08-30 05:44:55
一個簡單的方法是在輸出li標籤後立即設置一個變量。循環完成後,您可以檢查是否設置了該變量。如果不是,你知道沒有其他結果。 – 2010-08-30 05:48:26