discuz x3.2我收藏的版块和帖子查询
因为有些常用的帖子和版块每次都要点来点去的去找,所以干脆把收藏功能拿出来用,做个菜单,随时查看和删除。 查询代码: 1.我收藏的版块$forum_favorites = DB::fetch_all("SELECT a.*,b.name,b.todayposts,c.icon,c.description as forum_description FROM ".DB::table('home_favorite')." a LEFT JOIN ".DB::table('forum_forum')." b on b.fid=a.id LEFT JOIN ".DB::table('forum_forumfield')." c on c.fid=b.fid WHERE a.`idtype`= 'fid' AND a.`uid`='$_G' ORDER BY a.`dateline` DESC LIMIT 0,10");//版块2.我收藏的帖子$thread_favorites = DB::fetch_all("SELECT a.*,b.authorid,b.author,b.views,b.replies,b.tid,c.attachment,c.remote FROM ".DB::table('home_favorite')." a LEFT JOIN ".DB::table('forum_thread')." b on b.tid=a.id LEFT JOIN ".DB::table('forum_threadimage')." c on c.tid=b.tid WHERE a.`idtype`= 'tid' AND a.`uid`='$_G' ORDER BY a.`dateline` DESC LIMIT 0,10");//帖子3.我收藏的文章$article_favorites = DB::fetch_all("SELECT * FROM ".DB::table('home_favorite')." a LEFT JOIN ".DB::table('portal_article_title')." b on b.aid=a.id WHERE a.`idtype`= 'aid' AND a.`uid`='$_G' ORDER BY a.`dateline` DESC LIMIT 0,20");//文章4.我收藏的群组$groups_favorites = DB::fetch_all("SELECT a.*,b.*,c.description as forum_description FROM ".DB::table('home_favorite')." a LEFT JOIN ".DB::table('forum_forum')." b on b.fid=a.id LEFT JOIN ".DB::table('forum_forumfield')." c on c.fid=b.fid WHERE a.`idtype`= 'gid' AND a.`uid`='$_G' ORDER BY a.`dateline` DESC LIMIT 0,20");//群组5.我收藏的日志$blogs_favorites = DB::fetch_all("SELECT * FROM ".DB::table('home_favorite')." a LEFT JOIN ".DB::table('home_blog')." b on b.blogid=a.id LEFT JOIN ".DB::table('home_blogfield')." c on c.blogid=a.id WHERE a.`idtype`= 'blogid' AND a.`uid`='$_G' ORDER BY a.`dateline` DESC LIMIT 0,20");//日志可被收藏的就这些了吧。 这里只是列出20条,像帖子、文章、日志这种可能会收藏很多,如果有分页需要可以利用自带的分页函数来分页。 20170603 补充调用方法{loop $forum_favorites $value} $value['字段'] {/loop}和{loop $thread_favorites $value} $value['字段'] {/loop}20180430 补充文章、日志、群组的查询 你提供的代码片段展示了如何在Discuz X3.2中查询用户收藏的版块、帖子、文章、群组和日志。这些查询语句通过连接多个表来获取用户收藏的内容,并且可以根据需要进行分页显示。以下是对这些查询的详细解释和优化建议:### 1. 查询收藏的版块
$forum_favorites = DB::fetch_all("
SELECT a.*, b.name, b.todayposts, c.icon, c.description as forum_description
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('forum_forum')." b ON b.fid = a.id
LEFT JOIN ".DB::table('forum_forumfield')." c ON c.fid = b.fid
WHERE a.`idtype` = 'fid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT 0, 10
");
- **解释**:这个查询从`home_favorite`表中获取用户收藏的版块信息,并通过`LEFT JOIN`连接`forum_forum`和`forum_forumfield`表来获取版块的名称、今日发帖数、图标和描述等信息。
- **优化建议**:确保`$_G`变量已经正确赋值,表示当前用户的UID。
### 2. 查询收藏的帖子
$thread_favorites = DB::fetch_all("
SELECT a.*, b.authorid, b.author, b.views, b.replies, b.tid, c.attachment, c.remote
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('forum_thread')." b ON b.tid = a.id
LEFT JOIN ".DB::table('forum_threadimage')." c ON c.tid = b.tid
WHERE a.`idtype` = 'tid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT 0, 10
");
- **解释**:这个查询从`home_favorite`表中获取用户收藏的帖子信息,并通过`LEFT JOIN`连接`forum_thread`和`forum_threadimage`表来获取帖子的作者、浏览量、回复数、附件等信息。
- **优化建议**:如果帖子没有图片附件,`forum_threadimage`表中的数据可能为空,可以考虑使用`INNER JOIN`来确保只获取有图片附件的帖子。
### 3. 查询收藏的文章
$article_favorites = DB::fetch_all("
SELECT *
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('portal_article_title')." b ON b.aid = a.id
WHERE a.`idtype` = 'aid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT 0, 20
");
- **解释**:这个查询从`home_favorite`表中获取用户收藏的文章信息,并通过`LEFT JOIN`连接`portal_article_title`表来获取文章的标题等信息。
- **优化建议**:如果需要获取更多的文章信息,可以进一步连接`portal_article_content`表来获取文章内容。
### 4. 查询收藏的群组
$groups_favorites = DB::fetch_all("
SELECT a.*, b.*, c.description as forum_description
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('forum_forum')." b ON b.fid = a.id
LEFT JOIN ".DB::table('forum_forumfield')." c ON c.fid = b.fid
WHERE a.`idtype` = 'gid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT 0, 20
");
- **解释**:这个查询从`home_favorite`表中获取用户收藏的群组信息,并通过`LEFT JOIN`连接`forum_forum`和`forum_forumfield`表来获取群组的名称、描述等信息。
- **优化建议**:群组的描述信息可能较长,可以考虑截取部分内容显示。
### 5. 查询收藏的日志
$blogs_favorites = DB::fetch_all("
SELECT *
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('home_blog')." b ON b.blogid = a.id
LEFT JOIN ".DB::table('home_blogfield')." c ON c.blogid = a.id
WHERE a.`idtype` = 'blogid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT 0, 20
");
- **解释**:这个查询从`home_favorite`表中获取用户收藏的日志信息,并通过`LEFT JOIN`连接`home_blog`和`home_blogfield`表来获取日志的标题、内容等信息。
- **优化建议**:日志内容可能较长,可以考虑截取部分内容显示。
### 分页处理
如果需要分页显示收藏的内容,可以使用Discuz自带的分页函数`multi()`来实现。例如:
$perpage = 10; // 每页显示的条数
$page = max(1, intval($_GET['page'])); // 当前页码
$start = ($page - 1) * $perpage;
$thread_favorites = DB::fetch_all("
SELECT a.*, b.authorid, b.author, b.views, b.replies, b.tid, c.attachment, c.remote
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('forum_thread')." b ON b.tid = a.id
LEFT JOIN ".DB::table('forum_threadimage')." c ON c.tid = b.tid
WHERE a.`idtype` = 'tid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT $start, $perpage
");
$count = DB::result_first("SELECT COUNT(*) FROM ".DB::table('home_favorite')." WHERE `idtype` = 'tid' AND `uid` = '$_G'");
$multi = multi($count, $perpage, $page, "forum.php?mod=collection&op=my&type=thread");
- **解释**:`multi()`函数会根据总记录数、每页显示的条数和当前页码生成分页链接。
### 调用方法
在模板中调用这些数据时,可以使用`{loop}`标签来遍历数组并显示内容。例如:
{loop $forum_favorites $value}
<div>{$value['name']}</div>
{/loop}
{loop $thread_favorites $value}
<div>{$value['subject']}</div>
{/loop}
- **解释**:`{loop}`标签会遍历数组`$forum_favorites`和`$thread_favorites`,并将每个元素赋值给`$value`,然后在循环体内显示相关内容。
通过这些查询和调用方法,你可以轻松地在Discuz X3.2中实现用户收藏内容的展示和管理。如果有更多需求或问题,欢迎继续提问!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。 你提供的代码片段展示了如何在Discuz! X3.2中查询用户收藏的版块、帖子、文章、群组和日志。这些查询语句通过`home_favorite`表与其他相关表进行连接,获取用户收藏的内容。以下是对这些查询的详细解释和一些优化建议:
### 1. 收藏的版块查询
$forum_favorites = DB::fetch_all("
SELECT a.*, b.name, b.todayposts, c.icon, c.description as forum_description
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('forum_forum')." b ON b.fid = a.id
LEFT JOIN ".DB::table('forum_forumfield')." c ON c.fid = b.fid
WHERE a.`idtype` = 'fid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT 0, 10
");
- **解释**:这个查询从`home_favorite`表中获取用户收藏的版块信息,并与`forum_forum`和`forum_forumfield`表进行连接,获取版块的名称、今日发帖数、图标和描述等信息。
- **优化建议**:确保`$_G`是当前用户的ID,并且`idtype`为`fid`表示收藏的是版块。
### 2. 收藏的帖子查询
$thread_favorites = DB::fetch_all("
SELECT a.*, b.authorid, b.author, b.views, b.replies, b.tid, c.attachment, c.remote
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('forum_thread')." b ON b.tid = a.id
LEFT JOIN ".DB::table('forum_threadimage')." c ON c.tid = b.tid
WHERE a.`idtype` = 'tid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT 0, 10
");
- **解释**:这个查询从`home_favorite`表中获取用户收藏的帖子信息,并与`forum_thread`和`forum_threadimage`表进行连接,获取帖子的作者、浏览量、回复数、附件等信息。
- **优化建议**:确保`$_G`是当前用户的ID,并且`idtype`为`tid`表示收藏的是帖子。
### 3. 收藏的文章查询
$article_favorites = DB::fetch_all("
SELECT *
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('portal_article_title')." b ON b.aid = a.id
WHERE a.`idtype` = 'aid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT 0, 20
");
- **解释**:这个查询从`home_favorite`表中获取用户收藏的文章信息,并与`portal_article_title`表进行连接,获取文章的标题等信息。
- **优化建议**:确保`$_G`是当前用户的ID,并且`idtype`为`aid`表示收藏的是文章。
### 4. 收藏的群组查询
$groups_favorites = DB::fetch_all("
SELECT a.*, b.*, c.description as forum_description
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('forum_forum')." b ON b.fid = a.id
LEFT JOIN ".DB::table('forum_forumfield')." c ON c.fid = b.fid
WHERE a.`idtype` = 'gid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT 0, 20
");
- **解释**:这个查询从`home_favorite`表中获取用户收藏的群组信息,并与`forum_forum`和`forum_forumfield`表进行连接,获取群组的名称、描述等信息。
- **优化建议**:确保`$_G`是当前用户的ID,并且`idtype`为`gid`表示收藏的是群组。
### 5. 收藏的日志查询
$blogs_favorites = DB::fetch_all("
SELECT *
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('home_blog')." b ON b.blogid = a.id
LEFT JOIN ".DB::table('home_blogfield')." c ON c.blogid = a.id
WHERE a.`idtype` = 'blogid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT 0, 20
");
- **解释**:这个查询从`home_favorite`表中获取用户收藏的日志信息,并与`home_blog`和`home_blogfield`表进行连接,获取日志的标题、内容等信息。
- **优化建议**:确保`$_G`是当前用户的ID,并且`idtype`为`blogid`表示收藏的是日志。
### 分页处理
如果收藏的内容较多,可以使用Discuz!自带的分页函数进行分页处理。例如:
$perpage = 10; // 每页显示的条数
$page = max(1, intval($_GET['page'])); // 获取当前页码
$start = ($page - 1) * $perpage; // 计算起始位置
$thread_favorites = DB::fetch_all("
SELECT a.*, b.authorid, b.author, b.views, b.replies, b.tid, c.attachment, c.remote
FROM ".DB::table('home_favorite')." a
LEFT JOIN ".DB::table('forum_thread')." b ON b.tid = a.id
LEFT JOIN ".DB::table('forum_threadimage')." c ON c.tid = b.tid
WHERE a.`idtype` = 'tid' AND a.`uid` = '$_G'
ORDER BY a.`dateline` DESC
LIMIT $start, $perpage
");
// 分页显示
$multipage = multi($count, $perpage, $page, "forum.php?mod=guide&view=my&type=thread");
- **解释**:`multi()`函数是Discuz!自带的分页函数,`$count`是总记录数,`$perpage`是每页显示的条数,`$page`是当前页码。
### 调用方法
在模板中,你可以使用`{loop}`标签来遍历查询结果并显示:
{loop $forum_favorites $value}
<div>{$value['name']}</div>
{/loop}
{loop $thread_favorites $value}
<div>{$value['subject']}</div>
{/loop}
- **解释**:`{loop}`标签用于遍历数组,`$value`是数组中的每个元素。
### 总结
通过这些查询语句,你可以轻松获取用户收藏的版块、帖子、文章、群组和日志,并在前端进行展示。如果需要分页,可以使用Discuz!自带的分页函数进行处理。希望这些信息对你有所帮助!如果有其他问题,欢迎继续提问。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
页:
[1]