第三步
下面就开始来编写消息添加,删除,发布。基本逻辑很简单,是这样的,添加一条消息,则这条消息作为正在发布中的消息,只能有一条消息处于发布状态中(status=1),当选择消息列表的其他任意一条消息时,则update当前发布中的消息status=0,update所选择的消息status=1,创建表如下:
- DROP TABLE IF EXISTS cdb_my_notice;
- CREATE TABLE IF NOT EXISTS `cdb_my_notice` (
- `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
- `title` varchar(30) NOT NULL,
- `content` varchar(500) NOT NULL,
- `publish_time` varchar(30) NOT NULL,
- `status` tinyint(4) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
复制代码
然后在新建source/plugin/notice/add.php
- if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
- exit('Access Denied');
- }
- if (submitcheck('submit')) {
- $title = dhtmlspecialchars(trim($_G['gp_title']));
- $conent = daddslashes($_G['gp_content']);
- $publish_time = TIMESTAMP;
- DB::query("UPDATE " . DB::table('my_notice') . " SET status='0' WHERE status='1'");
- DB::query("INSERT INTO " . DB::table('my_notice') . "(title,content,publish_time,status) values('$title','$conent','$publish_time','1')");
- cpmsg("{$slang['success']}", '', 'succeed');
- }
- showformheader("plugins&operation=add&do=$pluginid&identifier=$identifier&pmod=admincp", $extra);
- echo "<div>{$slang['title']}</div>";
- showsetting('', 'title', '', 'text');
- echo "<div>{$slang['content']}</div>";
- showsetting('', 'content', '', 'textarea');
- showsubmit('submit', $slang['submit']);
- showformfooter();
复制代码
解释一下上面的代码
submitcheck() 函数用于判断表单提交按钮
$_G[gp_title] 其实就是$_POST['title'],为什么是这个样子,其实在sorce/class/class_core.php中可以找到答案,请看其中的一句代码就知道了
- if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST)) {
- $_GET = array_merge($_GET, $_POST);
- }
- if(isset($_GET['diy'])) {
- $_GET['diy'] = empty($_GET['diy']) ? '' : $_GET['diy'];
- }
- foreach($_GET as $k => $v) {
- $this->var['gp_'.$k] = $v;
- }
复制代码
代码意图很明显,不解释,sorce/class/class_core.php这个文件很重要,一定要看一下,对你做二次开发或者插件开发很有帮助。
DB::query()就是执行SQL语句,其他还有常用的DB::fetch(),DB::fetch_first(),DB::table(),DB::update()等等这些操作数据库的函数一看名字就知道了
关于以show开头的几个函数就是构建一个表单,具体几个函数的用法和解释请看我的《Discuz!X 二次开发之后台HTML显示函数方法以及使用》,下面讲到的代码段里面用到类似的函数就不解释了。
打开”消息提示“插件的”管理“就会看到一下页面了,同时点击添加数据就可以添加到数据库了。
然后我们再开始编写消息列表,其中可以删除消息和更换发布的消息。
- if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
- exit('Access Denied');
- }
- if($_G['gp_pid']) //更改发布消息
- {
- $id = intval($_G['gp_pid']);
- DB::query("UPDATE ".DB::table('my_notice')." SET status=0 WHERE status=1");
- DB::query("UPDATE ".DB::table('my_notice')." SET status=1 WHERE id='$id'");
- cpmsg("{$slang['edit_success']}", '', 'succeed');
- }
- if(submitcheck('delete')) //批量删除
- {
- $ids = dimplode($_G['gp_delete']);
- DB::query("DELETE FROM " . DB::table('my_notice') . " WHERE id IN ($ids)");
- }
- showformheader("plugins&operation=list&do=$pluginid&identifier=$identifier&pmod=admincp");
- showtableheader("{$slang['notice_list']}", 'action=plugins&operation=list&do=$pluginid&identifier=$identifier&pmod=admincp', '', 5);
- showtablerow('',
- array(
- 'class="header"',
- 'class="header"',
- 'class="header"',
- 'class="header"',
- 'class="header"'
- ),
- array(
- '',
- $slang['title'],
- $slang['content'],
- $slang['publish_time'],
- $slang['status']
- )
- );
- $query = DB::query("SELECT * FROM " . DB::table('my_notice') . " ORDER BY publish_time DESC");
- while ($result = DB::fetch($query)) {
- showtablerow('',
- array(
- ),
- array(
- '<input type="checkbox" class="checkbox" name="delete[]" value="'.$result['id'].'" />',
- $result['title'],
- $result['content'],
- date('Y-m-d h:i:s', $result['publish_time']),
- $result['status'] == 0 ? "<a href='{$_G['basefilename']}?action=plugins&operation=list&do=$pluginid&identifier=$identifier&pmod=admincp&pid={$result['id']}' > {$slang['publish']}</a> " : $slang['in_publish']
- )
- );
- showhiddenfields(array('id' => $result['id']));
- }
- showtablefooter();
- showsubmit('submit', 'submit', 'del');
- showformfooter();
复制代码
这里逻辑很简单,不解释,具体show开头的函数都是为了构建表单和表格用的,要是你从头开始看这个教程的话理解起来应该没问题了。
showformheader("plugins&operation=list&do=$pluginid&identifier=$identifier&pmod=admincp");这就是表单提交的路径,
会自动生成action=‘action=plugins&operation=list&do=0&identifier=$identifier&pmod=admincp’的post表单,
$pluginid为该插件的id号,在我们第一步创建这个插件的时候已经生成了,$identifier就是这个插件了
这样我们消息的管理功能基本就实现了。
(转载请标明出处:Discuz!X 插件开发实例教程)
|