Discuz教程网

一个模拟discuz发帖的php类

[复制链接]
authicon dly 发表于 2013-10-31 11:53:16 | 显示全部楼层 |阅读模式
一个模拟discuz发帖的php类,discuz发帖流程请查看:https://www.discuz.1314study.com/t/102250.html
代码:
  1. <?php
  2. date_default_timezone_set('PRC');
  3. include 'db.class.php';

  4. class post
  5. {
  6.         private $host = 'localhost';
  7.         private $username = 'root';
  8.         private $password = '123456';
  9.         private $database_name = 'ultrax';
  10.         private $_db = null;

  11.         private $fid = null;
  12.         private $title = null;
  13.         private $content = null;
  14.         private $author = null;
  15.         private $author_id = null;
  16.         private $current_time = null;
  17.         private $displayorder = null; //0:正常,-2:待审核

  18.         public function __construct($fid, $title, $content, $author, $author_id, $displayorder = -2)
  19.         {
  20.                 $this->_db = db::get_instance($this->host, $this->username, $this->password, $this->database_name);

  21.                 $this->fid          = $fid;
  22.                 $this->title        = $title;
  23.                 $this->content      = $content;
  24.                 $this->author       = $author;
  25.                 $this->author_id    = $author_id;
  26.                 $this->current_time = time();
  27.                 $this->displayorder = $displayorder;
  28.         }

  29.         public function post_posts()
  30.         {
  31.                 $tid = $this->do_pre_forum_thread();

  32.                 if(!$tid)
  33.                 {
  34.                         return '更新表 pre_forum_thread 失败<br />';
  35.                 }

  36.                 $pid = $this->do_pre_forum_post_tableid();

  37.                 if(!$this->do_pre_forum_post($pid, $tid))
  38.                 {
  39.                         return '更新表 pre_forum_post 失败<br />';
  40.                 }

  41.                 if(!$this->do_pre_forum_forum())
  42.                 {
  43.                         return '更新表 pre_forum_forum 失败<br />';
  44.                 }

  45.                 if($this->displayorder == -2)
  46.                 {
  47.                         if(!($this->do_pre_forum_thread_moderate($tid)))
  48.                         {
  49.                                 return '更新表 pre_forum_thread_moderate 失败<br />';
  50.                         }
  51.                 }

  52.                 return ($this->do_pre_common_member_count()) ? '发帖成功<br />' : '更新表 pre_pre_common_member_count 失败<br />';
  53.         }

  54.         private function do_pre_forum_thread()
  55.         {
  56.                 $data                 = array();
  57.                 $data['fid']          = $this->fid;
  58.                 $data['author']       = $this->author;
  59.                 $data['authorid']     = $this->author_id;
  60.                 $data['subject']      = $this->title;
  61.                 $data['dateline']     = $this->current_time;
  62.                 $data['lastpost']     = $this->current_time;
  63.                 $data['lastposter']   = $this->author;
  64.                 $data['displayorder'] = $this->displayorder;

  65.                 $result = $this->_db->insert($data, 'pre_forum_thread');

  66.                 if($result == 1)
  67.                 {
  68.                         $tid = $this->get_last_id();
  69.                 }

  70.                 return $tid;
  71.         }

  72.         private function do_pre_forum_post_tableid()
  73.         {
  74.                 $sql    = "INSERT INTO `pre_forum_post_tableid`(`pid`) VALUES(NULL)";
  75.                 $result = $this->_db->query($sql);
  76.                 if($result == 1)
  77.                 {
  78.                         $pid = $this->get_last_id();
  79.                 }

  80.                 return $pid;
  81.         }

  82.         private function do_pre_forum_post($pid, $tid)
  83.         {
  84.                 $data             = array();
  85.                 $data['pid']      = $pid;
  86.                 $data['fid']      = $this->fid;
  87.                 $data['tid']      = $tid;
  88.                 $data['first']    = 1;
  89.                 $data['author']   = $this->author;
  90.                 $data['authorid'] = $this->author_id;
  91.                 $data['subject']  = $this->title;
  92.                 $data['dateline'] = $this->current_time;
  93.                 $data['message']  = $this->content;

  94.                 $result = $this->_db->insert($data, 'pre_forum_post');

  95.                 return ($result == 1) ? true : false;
  96.         }

  97.         private function do_pre_forum_forum()
  98.         {
  99.                 $sql = "UPDATE `pre_forum_forum` SET `threads`=threads+1,`posts`=posts+1,`todayposts`=todayposts+1 WHERE `fid`={$this->fid}";

  100.                 $result = $this->_db->query($sql);

  101.                 return ($result == 1) ? true : false;
  102.         }

  103.         private function do_pre_forum_thread_moderate($tid)
  104.         {
  105.                 $data             = array();
  106.                 $data['tid']      = $tid;
  107.                 $data['status']   = 0;
  108.                 $data['dateline'] = $this->current_time;

  109.                 $result = $this->_db->insert($data, 'pre_forum_thread_moderate');

  110.                 return ($result == 1) ? true : false;
  111.         }

  112.         private function do_pre_common_member_count()
  113.         {
  114.                 $sql = "UPDATE `pre_common_member_count` SET `threads`=threads+1 WHERE `uid`={$this->author_id}";

  115.                 $result = $this->_db->query($sql);

  116.                 return ($result == 1) ? true : false;
  117.         }

  118.         private function get_last_id()
  119.         {
  120.                 $sql    = "SELECT LAST_INSERT_ID()";
  121.                 $result = mysql_query($sql);

  122.                 while($row = mysql_fetch_assoc($result))
  123.                 {

  124.                         $id = $row['LAST_INSERT_ID()'];
  125.                 }

  126.                 return $id;
  127.         }
  128. }
复制代码

来自:http://blog.php230.com/an-analog-discuz-posting-php-class.html



上一篇:discuz数据采集发帖
下一篇:让discuz支持大附件上传
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 16:59

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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