您现在的位置是:首页 > cms教程 > Discuz教程Discuz教程
discuz帖子模块用到的表及自动发帖函数介绍
寒云2025-06-27Discuz教程已有人查阅
导读最近在做一个discuz的插件,由于需要程序自动生成并调用discuz已经存在插件的帖子。然而这就相当于自动发帖的功能了。网上找了一下,大部分都是通过curl模拟登陆,模拟发帖的
最近在做一个discuz的插件,由于需要程序自动生成并调用discuz已经存在插件的帖子。然而这就相当于自动发帖的功能了。网上找了一下,大部分都是通过curl模拟登陆,模拟发帖的,这显然不满足我的要求。如果采用这种方式既笨重又麻烦。百度了一通,没发现好的结果。于是google了一番,之后找到一个类似的方法。经过一番整理,于是有了下面这个函数。
discuz帖子模块用到的表:
帖子表:pre_forum_post
帖子表pid较大值设置表:pre_forum_post_tableid
帖子列表表:pre_forum_thread
帖子所在板块表:pre_forum_forum
这几个表之间的关系是,帖子表pre_forum_post存放帖子的详细信息,其pid通过pre_forum_post_tableid表获得。帖子列表pre_forum_thread表决定了该条记录是否显示在列表中,如果此表中没有相应的记录帖子也就无法显示在列表中了。帖子所在板块表pre_forum_forum存放了对应板块的发帖数量,今日发帖数以及最近发帖的标题等信息。
好了,了解了这几张表之间的关系后有了下面这个函数和测试例子。
如果是插件这里有个需要特别注意的地方。
1、forum_thread表,必须将special字段的值设为127($thread["special"]= 127;)
2、forum_post表的message字段。如果你的是插件的话,之后面一定要加上
原因分析:
./source/module/forum/forum_viewthread.php,大概700行左右,有这么一段
1、special为127是才执行插件的内容
2、由于插件有很多,真么知道是哪个插件呢?因此,请看这行
discuz帖子模块用到的表:
帖子表:pre_forum_post
帖子表pid较大值设置表:pre_forum_post_tableid
帖子列表表:pre_forum_thread
帖子所在板块表:pre_forum_forum
这几个表之间的关系是,帖子表pre_forum_post存放帖子的详细信息,其pid通过pre_forum_post_tableid表获得。帖子列表pre_forum_thread表决定了该条记录是否显示在列表中,如果此表中没有相应的记录帖子也就无法显示在列表中了。帖子所在板块表pre_forum_forum存放了对应板块的发帖数量,今日发帖数以及最近发帖的标题等信息。
好了,了解了这几张表之间的关系后有了下面这个函数和测试例子。
<?php
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
echo "11111111111111111111111";
require_once DISCUZ_ROOT . './source/class/class_core.php';
$discuz = C::app();
$discuz->cachelist = $cachelist;
$discuz->init();
$subject = '自行写入帖子';
$message = '自行写入帖子的消息消息消息';
$thread['fid'] = 86;
$thread['subject'] = $subject;
$thread['message'] = $message;
$thread["authorid"]= 1;
$thread["author"]=$_G['member'][username];
$tid = addThread($thread);
echo "*********** $tid **************";
/*
* 添加帖子
* @data 帖子数组
* array('fid' => '板块ID',
'subject' => '标题',
'message' => '具体内容',
'authorid' => '用户ID',
'author' => '用户名');
*/
function addThread($data){
$fid = $data['fid'];
$subject = $data['subject'];
$message = $data['message'];
$authorid = $data['authorid'];
$author = $data['author'];
$proc_time = time();
$thread['fid'] = $fid;//板块ID
$thread['subject'] = $subject;//标题
$thread["authorid"]= $authorid;
$thread["author"]= $author;
$thread["dateline"]= $proc_time;
$thread["lastpost"]= $proc_time;
$thread["lastposter"]= $author;
//插件必须添加special参数,否则无法按照插件的样式进行显示
$thread["special"]= 127;//特殊主题,1:投票;2:商品;3:悬赏;4:活动;5:辩论贴;127:插件相关
$tid=C::t('forum_thread')->insert($thread, 1);//添加到帖子列表
if($tid){
$post["tid"] = $tid;
$post['fid'] = $fid;
$post["position"]= 1;
$post["smileyoff"]= "-1";
$post["bbcodeoff"]= "-1";
$post["usesig"]= 1;
$post["author"]= $author;
$post["authorid"]= $authorid;
$post["subject"]= $subject;
$post["message"]= $message . chr(0).chr(0).chr(0) . "online_alipay";//结尾一定要加chr(0).chr(0).chr(0) . "online_alipay"结尾,否则无法调用插件的模板。
$post["dateline"]= $proc_time;
$post["first"]= 1;
$pid = C::t('forum_post_tableid')->insert(array('pid' => null), true);//添加pre_forum_post表的pid较大值
$post["pid"]= $pid;
$okid=C::t('forum_post')->insert("0", $post, 1);//写入帖子
$lastpost = $tid . "$subject" . $proc_time . $author;
$sql = "update " . DB::table('forum_forum') . " set threads = threads + 1, todayposts = todayposts + 1, lastpost = '$lastpost' where fid= $fid";//修改今日主题和帖子数量
DB::query($sql);
}
return $tid;
}
?>
addThread参数需要提供几个必要的参数板块ID、标题、用户名、用户ID和消息内容。如果你想往哪个板块自动生成一个帖子,尽管调用addThread函数即可。如果是插件这里有个需要特别注意的地方。
1、forum_thread表,必须将special字段的值设为127($thread["special"]= 127;)
2、forum_post表的message字段。如果你的是插件的话,之后面一定要加上
chr(0).chr(0).chr(0) . "插件名称"
否则,插件的模板将无法调用。这是为什么呢?这涉及到discuz插件模板设计的问题。原因分析:
./source/module/forum/forum_viewthread.php,大概700行左右,有这么一段
if($_G['forum_thread']['special'] > 0 && (empty($_GET['viewpid']) || $_GET['viewpid'] == $_G['forum_firstpid'])) {
$_G['forum_thread']['starttime'] = gmdate($_G['forum_thread']['dateline']);
$_G['forum_thread']['remaintime'] = '';
switch($_G['forum_thread']['special']) {
case 1: require_once libfile('thread/poll', 'include'); break;
case 2: require_once libfile('thread/trade', 'include'); break;
case 3: require_once libfile('thread/reward', 'include'); break;
case 4: require_once libfile('thread/activity', 'include'); break;
case 5: require_once libfile('thread/debate', 'include'); break;
case 127:
if($_G['forum_firstpid']) {
$sppos = strpos($postlist[$_G['forum_firstpid']]['message'], chr(0).chr(0).chr(0));
$specialextra = substr($postlist[$_G['forum_firstpid']]['message'], $sppos + 3);
$postlist[$_G['forum_firstpid']]['message'] = substr($postlist[$_G['forum_firstpid']]['message'], 0, $sppos);
if($specialextra) {
if(array_key_exists($specialextra, $_G['setting']['threadplugins'])) {
@include_once DISCUZ_ROOT.'./source/plugin/'.$_G['setting']['threadplugins'][$specialextra]['module'].'.class.php';
$classname = 'threadplugin_'.$specialextra;
if(class_exists($classname) && method_exists($threadpluginclass = new $classname, 'viewthread')) {
$threadplughtml = $threadpluginclass->viewthread($_G['tid']);
//var_dump($post['message']);
}
}
}
}
break;
}
}
原因就出在这。1、special为127是才执行插件的内容
2、由于插件有很多,真么知道是哪个插件呢?因此,请看这行
$sppos = strpos($postlist[$_G['forum_firstpid']]['message'], chr(0).chr(0).chr(0));
discuz采用了chr(0).chr(0).chr(0)进行分割,获取插件名。如果无法获取插件名,则无法调用相应的模板,因而也就调用默认的系统模板了。
本文标签:
很赞哦! ()
下一篇:discuz数据表结构介绍
图文教程
Discuz!NT怎么安装到网站子目录
最近需要把bbs整合到一个网站上,之后选了Discuz!NT,因为是asp.net开发的,感觉以后同网站整合会方便些,下面是我的安装心得。假设网站的域名是:http://test/, 论坛安装到以下地址:http://test/bbs
discuz的SSO单点,同歩,异步登录实现方法
discuz(dz) SSO(单点,同歩,异步)登录1 )登录请求到 localhost/member.php 初始化一些设置,然后调用source/module/member/member_logging.php(37行)。
discuz添加兼容html5标签音乐播放器的办法
关于在discuz中mp3附件自动添加Windows Media播放器插件的方法的文章网上有很多,只要修改./templates/default/discuzcode.htm文件就可以了
discuz自定义生成单页面的方法
在pc端,若要生成一个单页面,有一个比较方便的方法是生成新的专题页,然后diy其中的内容。不过这种做法有两个缺点1 url太过冗赘2 只有一个插入url代码功能,没有文本编辑功能
相关源码
-
(自适应手机端)英文外贸电子产品通用pbootcms模板源码下载为电子产品外贸企业设计的响应式网站模板,采用PbootCMS开发内核,支持多语言展示。模板默认集成产品展示系统、询价表单模块和企业资质展示区,满足跨境贸易基础需求。整站采用模块化设计,便于扩展业务场景。查看源码 -
(自适应响应式)工业机床工程农业机械设备网站源码下载框架适用于工程机械、机床设备等工业领域。通过模块调整可快速转型为农业机械、物流设备展示系统。预留7种工业产品展示模板。查看源码 -
(自适应)帝国cms7.5文章新闻博客整站源码( 带会员中心)本模板基于帝国CMS内核开发,为新闻资讯、个人博客及作品展示类网站设计。采用响应式布局技术,确保在手机、平板和电脑等不同设备上都能获得良好的浏览体验。查看源码 -
(自适应)WordPress二次元博客主题SakurairoSakurairo主题为二次元内容创作者设计,提供丰富的动漫风格元素和个性化的展示效果。该主题在原有Sakura主题基础上进行了功能增强,支持多种自定义设置,满足动漫爱好者建立个人博客的需求。查看源码 -
(自适应响应式)绿色环保材料设备科技类营销型网站pbootcms源码下载本模板基于PbootCMS开发,主要面向环保设备、环保材料及相关科技企业。采用HTML5+CSS3技术构建,具备响应式特性,确保在各类设备上均有良好展示效果。查看源码 -
帝国cms大气淘宝客网站源码带手机版带火车头采集本款创意礼物导购网站模板为礼物类电商平台设计,采用清爽简约的界面风格,具备完善的商品导购功能。系统支持在文章攻略中灵活插入商品购买链接,实现内容与电商的结合。查看源码
| 分享笔记 (共有 篇笔记) |
