我在我的网站上有这个设计。我的模块包括:新闻、文章、视频、照片、下载、评论、测验、投票等等,每个模块都在单独的表格中。我有一个喜欢的表格,用户可以喜欢或不喜欢一篇文章(在你的情况下是收藏)。获取这些内容的查询并不复杂。
首先,大多数模块的表格结构基本相同:
- id
- title
- content
- user_id(作者)
- date
- 等等
有一些例外,比如有时标题被称为问题,或者没有内容列。但这并不会引起任何问题。
我的喜欢表格设置如下:
- id
- page_id
- module_id(它来自哪个表格...我有一个模块表格,每个模块都有一个标题、关联id、目录等)
- post_id(对应于模块表格的id)
- user_id(进行喜欢或发布的用户)
- status(0=喜欢,1=不喜欢)
- date(喜欢/不喜欢发生的日期)
模块表格示例:
- id
- title
- directory
- post_type
示例
id title directory post_type
1 News news news
2 Episode Guide episodes episode
3 Albums discography/albums album
基本上,您的设置将类似,根据您的需求修改表结构。
查询特定用户所有点赞或收藏的内容:
$getlikes = mysql_query("SELECT DISTINCT post_id, module_id, page_id FROM likes WHERE user_id = $profile_id ORDER BY id DESC LIMIT $offset, $likes_limit", $conn);
$likes = mysql_num_rows($getlikes);
if($likes == "0"){
echo "<br><Center>$profile_username does not have any liked posts at this time.</center><BR>";
}
else {
echo "<table width='100%' cellspacing='0' cellpadding='5'>
<Tr><th>Post</th><th align='center'>Module</th><th align='center'>Page</th><tr>";
while ($rowlikes = mysql_fetch_assoc($getlikes)) {
// echo data
$like_page_id = $rowlikes['page_id'];
$like_module_id = $rowlikes['module_id'];
$like_post_id = $rowlikes['post_id'];
// different modules have different fields for the "title", most are called title but quotes is called "content" and polls is called "questions"
if($like_module_id == "11"){
$field = "question";
}
elseif($like_module_id == "19"){
$field = "content";
}
else{
$field = "title";
}
// FUNCTIONS
PostURL($like_page_id, $like_module_id, $like_post_id);
ModTitle($like_module_id);
ModTable($like_module_id);
ModURL($like_page_id, $like_module_id);
fpgURL($like_page_id);
$getpostinfo = mysql_query("SELECT $field AS field FROM $mod_table WHERE id = $like_post_id", $conn);
$rowpostinfo = mysql_fetch_assoc($getpostinfo);
$like_post_title = $rowpostinfo['field'];
// Using my "tiny" function to shorten the title if the module is "Quotes"
if($like_module_id == "19"){
Tiny($like_post_title, "75");
$like_post_title = "\"$tiny\"";
}
if(!$like_post_title){
$like_post_title = "<i>Unknown</i>";
}
else {
$like_post_title = "<a href='$post_url'>$like_post_title</a>";
}
echo "<tr class='$altrow'>
<td>$like_post_title</td>
<td align='center'><a href='$mod_url'>$mod_title</a></td>
<td align='center'>$fpg_url</td>
</tr>";
$altrow = ($altrow == 'altrow')?'':'altrow';
} // end while
echo "<tr><Td align='center' colspan='3'>";
// FUNCTIONS - Pagination links
PaginationLinks("$cs_url/users/$profile_id", "likes");
echo "</td></tr></table>";
} // end else if no likes
可能对你来说有些难以理解,因为我有很多自己的变量,但基本上它从likes表中获取模块id和帖子id,然后运行查询以获取帖子的标题和其他任何我想要的信息,例如原始作者。
我设置了“模块”功能,如果你提供其id,它将返回模块的URL或标题。