Discuz教程网

[插件制作] Discuz X 插件开发实例教程-右下角弹出广告框(三)

[复制链接]
authicon dly 发表于 2011-10-6 10:37:37 | 显示全部楼层 |阅读模式
第三步

下面就开始来编写消息添加,删除,发布。基本逻辑很简单,是这样的,添加一条消息,则这条消息作为正在发布中的消息,只能有一条消息处于发布状态中(status=1),当选择消息列表的其他任意一条消息时,则update当前发布中的消息status=0,update所选择的消息status=1,创建表如下:

  1. DROP TABLE IF EXISTS cdb_my_notice;
  2. CREATE TABLE IF NOT EXISTS `cdb_my_notice` (
  3. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  4. `title` varchar(30) NOT NULL,
  5. `content` varchar(500) NOT NULL,
  6. `publish_time` varchar(30) NOT NULL,
  7. `status` tinyint(4) NOT NULL,
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
复制代码


然后在新建source/plugin/notice/add.php

  1. if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
  2.     exit('Access Denied');
  3. }

  4. if (submitcheck('submit')) {
  5.     $title = dhtmlspecialchars(trim($_G['gp_title']));
  6.     $conent = daddslashes($_G['gp_content']);
  7.     $publish_time = TIMESTAMP;
  8.     DB::query("UPDATE " . DB::table('my_notice') . " SET status='0' WHERE status='1'");
  9.     DB::query("INSERT INTO " . DB::table('my_notice') . "(title,content,publish_time,status) values('$title','$conent','$publish_time','1')");
  10.     cpmsg("{$slang['success']}", '', 'succeed');
  11. }

  12. showformheader("plugins&operation=add&do=$pluginid&identifier=$identifier&pmod=admincp", $extra);
  13. echo "<div>{$slang['title']}</div>";
  14. showsetting('', 'title', '', 'text');
  15. echo "<div>{$slang['content']}</div>";
  16. showsetting('', 'content', '', 'textarea');
  17. showsubmit('submit', $slang['submit']);
  18. showformfooter();
复制代码

解释一下上面的代码

submitcheck() 函数用于判断表单提交按钮

$_G[gp_title] 其实就是$_POST['title'],为什么是这个样子,其实在sorce/class/class_core.php中可以找到答案,请看其中的一句代码就知道了

  1. if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST)) {
  2.                         $_GET = array_merge($_GET, $_POST);
  3.                 }

  4.                 if(isset($_GET['diy'])) {
  5.                         $_GET['diy'] = empty($_GET['diy']) ? '' : $_GET['diy'];
  6.                 }

  7.                 foreach($_GET as $k => $v) {
  8.                         $this->var['gp_'.$k] = $v;
  9.                 }
复制代码

代码意图很明显,不解释,sorce/class/class_core.php这个文件很重要,一定要看一下,对你做二次开发或者插件开发很有帮助。
DB::query()就是执行SQL语句,其他还有常用的DB::fetch(),DB::fetch_first(),DB::table(),DB::update()等等这些操作数据库的函数一看名字就知道了

关于以show开头的几个函数就是构建一个表单,具体几个函数的用法和解释请看我的《Discuz!X 二次开发之后台HTML显示函数方法以及使用》,下面讲到的代码段里面用到类似的函数就不解释了。

打开”消息提示“插件的”管理“就会看到一下页面了,同时点击添加数据就可以添加到数据库了。
5.bmp
然后我们再开始编写消息列表,其中可以删除消息和更换发布的消息。

  1. if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
  2.     exit('Access Denied');
  3. }

  4. if($_G['gp_pid']) //更改发布消息
  5. {
  6.     $id = intval($_G['gp_pid']);
  7.     DB::query("UPDATE ".DB::table('my_notice')." SET status=0 WHERE status=1");
  8.     DB::query("UPDATE ".DB::table('my_notice')." SET status=1 WHERE id='$id'");
  9.     cpmsg("{$slang['edit_success']}", '', 'succeed');
  10. }

  11. if(submitcheck('delete')) //批量删除
  12. {
  13.     $ids = dimplode($_G['gp_delete']);
  14.     DB::query("DELETE FROM " . DB::table('my_notice') . " WHERE id IN ($ids)");
  15. }

  16. showformheader("plugins&operation=list&do=$pluginid&identifier=$identifier&pmod=admincp");
  17. showtableheader("{$slang['notice_list']}", 'action=plugins&operation=list&do=$pluginid&identifier=$identifier&pmod=admincp', '', 5);
  18. showtablerow('',
  19.         array(
  20.             'class="header"',
  21.             'class="header"',
  22.             'class="header"',
  23.             'class="header"',
  24.             'class="header"'
  25.         ),
  26.         array(
  27.             '',
  28.             $slang['title'],
  29.             $slang['content'],
  30.             $slang['publish_time'],
  31.             $slang['status']
  32.         )
  33. );

  34. $query = DB::query("SELECT * FROM " . DB::table('my_notice') . " ORDER BY publish_time DESC");
  35. while ($result = DB::fetch($query)) {
  36.     showtablerow('',
  37.             array(
  38.             ),
  39.             array(
  40.                 '<input type="checkbox" class="checkbox" name="delete[]"  value="'.$result['id'].'" />',
  41.                 $result['title'],
  42.                 $result['content'],
  43.                 date('Y-m-d h:i:s', $result['publish_time']),
  44.                 $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']
  45.             )
  46.     );
  47.     showhiddenfields(array('id' => $result['id']));
  48. }

  49. showtablefooter();
  50. showsubmit('submit', 'submit', 'del');
  51. 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就是这个插件了
6.bmp

这样我们消息的管理功能基本就实现了。

(转载请标明出处:Discuz!X 插件开发实例教程)



上一篇:Discuz X 插件开发实例教程-右下角弹出广告框(二)
下一篇:Discuz X 插件开发实例教程-右下角弹出广告框(四)
authicon 宿迁 发表于 2012-8-19 18:10:03 | 显示全部楼层
生是她的人,死是她的吉祥物。
authicon 绝对零度 发表于 2012-11-9 13:26:00 | 显示全部楼层
强烈支持楼主ing……我只说两句……
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 01:25

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表