每次用戶閱讀帖子,它都會分配一個cookie,例如,正在搜索數據庫中的未讀帖子
set_cookies($id,'read',60*60*24);
但問題是如何選擇所有未被用戶閱讀的帖子?
SELECT * from posts where (post is unread)
它不需要登錄。表結構:
ID | Content | Category
每次用戶閱讀帖子,它都會分配一個cookie,例如,正在搜索數據庫中的未讀帖子
set_cookies($id,'read',60*60*24);
但問題是如何選擇所有未被用戶閱讀的帖子?
SELECT * from posts where (post is unread)
它不需要登錄。表結構:
ID | Content | Category
有了您的解決方案,你會做這樣的事情:
$ids = array();
if (isset($_COOKIES)) {
foreach ($_COOKIES as $cookie => $value) {
if (is_numeric($cookie) && $value == 'read') {
$ids[] = $cookie;
}
}
}
if (isset($ids[0])) {
$posts = implode(',',$ids);
$query = "SELECT * from posts where id in ({$posts})";
// Do the query
} else {
// no read posts.
}
但你真的應該考慮存儲你讀的變量不同。
如果您可以創建已讀取ID的列表,是:
SELECT *
FROM posts
WHERE ID NOT IN ($list_of_post_ids_that_have_been_read)
我假設在這裏,當用戶閱讀後存儲在讀帖子的ID某處。讓我們暫時假定它是在具有格式表read_posts
:
UID | ID
在這種情況下,您的查詢就會變成:
SELECT * FROM posts WHERE ID NOT IN (SELECT id FROM read_posts WHERE uid = <user's id>);
如果只允許讀取在同一順序和存儲數據表查詢變得更簡單:
SELECT p.* FROM posts p, read_posts rp WHERE p.ID > rp.ID AND rp.UID = <user id>;
此查詢的語法可能略有不同,但我認爲一般的想法很清楚。
請給我們一些關於表結構的細節。 – 2011-12-20 22:10:30
更新了它的伴侶:) – Michelle 2011-12-20 22:14:27
你把什麼東西放在cookie中?帖子ID? – 2011-12-20 22:17:17