代码: - <?php
- date_default_timezone_set('PRC');
- include 'db.class.php';
- class post
- {
- private $host = 'localhost';
- private $username = 'root';
- private $password = '123456';
- private $database_name = 'ultrax';
- private $_db = null;
- private $fid = null;
- private $title = null;
- private $content = null;
- private $author = null;
- private $author_id = null;
- private $current_time = null;
- private $displayorder = null; //0:正常,-2:待审核
- public function __construct($fid, $title, $content, $author, $author_id, $displayorder = -2)
- {
- $this->_db = db::get_instance($this->host, $this->username, $this->password, $this->database_name);
- $this->fid = $fid;
- $this->title = $title;
- $this->content = $content;
- $this->author = $author;
- $this->author_id = $author_id;
- $this->current_time = time();
- $this->displayorder = $displayorder;
- }
- public function post_posts()
- {
- $tid = $this->do_pre_forum_thread();
- if(!$tid)
- {
- return '更新表 pre_forum_thread 失败<br />';
- }
- $pid = $this->do_pre_forum_post_tableid();
- if(!$this->do_pre_forum_post($pid, $tid))
- {
- return '更新表 pre_forum_post 失败<br />';
- }
- if(!$this->do_pre_forum_forum())
- {
- return '更新表 pre_forum_forum 失败<br />';
- }
- if($this->displayorder == -2)
- {
- if(!($this->do_pre_forum_thread_moderate($tid)))
- {
- return '更新表 pre_forum_thread_moderate 失败<br />';
- }
- }
- return ($this->do_pre_common_member_count()) ? '发帖成功<br />' : '更新表 pre_pre_common_member_count 失败<br />';
- }
- private function do_pre_forum_thread()
- {
- $data = array();
- $data['fid'] = $this->fid;
- $data['author'] = $this->author;
- $data['authorid'] = $this->author_id;
- $data['subject'] = $this->title;
- $data['dateline'] = $this->current_time;
- $data['lastpost'] = $this->current_time;
- $data['lastposter'] = $this->author;
- $data['displayorder'] = $this->displayorder;
- $result = $this->_db->insert($data, 'pre_forum_thread');
- if($result == 1)
- {
- $tid = $this->get_last_id();
- }
- return $tid;
- }
- private function do_pre_forum_post_tableid()
- {
- $sql = "INSERT INTO `pre_forum_post_tableid`(`pid`) VALUES(NULL)";
- $result = $this->_db->query($sql);
- if($result == 1)
- {
- $pid = $this->get_last_id();
- }
- return $pid;
- }
- private function do_pre_forum_post($pid, $tid)
- {
- $data = array();
- $data['pid'] = $pid;
- $data['fid'] = $this->fid;
- $data['tid'] = $tid;
- $data['first'] = 1;
- $data['author'] = $this->author;
- $data['authorid'] = $this->author_id;
- $data['subject'] = $this->title;
- $data['dateline'] = $this->current_time;
- $data['message'] = $this->content;
- $result = $this->_db->insert($data, 'pre_forum_post');
- return ($result == 1) ? true : false;
- }
- private function do_pre_forum_forum()
- {
- $sql = "UPDATE `pre_forum_forum` SET `threads`=threads+1,`posts`=posts+1,`todayposts`=todayposts+1 WHERE `fid`={$this->fid}";
- $result = $this->_db->query($sql);
- return ($result == 1) ? true : false;
- }
- private function do_pre_forum_thread_moderate($tid)
- {
- $data = array();
- $data['tid'] = $tid;
- $data['status'] = 0;
- $data['dateline'] = $this->current_time;
- $result = $this->_db->insert($data, 'pre_forum_thread_moderate');
- return ($result == 1) ? true : false;
- }
- private function do_pre_common_member_count()
- {
- $sql = "UPDATE `pre_common_member_count` SET `threads`=threads+1 WHERE `uid`={$this->author_id}";
- $result = $this->_db->query($sql);
- return ($result == 1) ? true : false;
- }
- private function get_last_id()
- {
- $sql = "SELECT LAST_INSERT_ID()";
- $result = mysql_query($sql);
- while($row = mysql_fetch_assoc($result))
- {
- $id = $row['LAST_INSERT_ID()'];
- }
- return $id;
- }
- }
复制代码
来自:http://blog.php230.com/an-analog-discuz-posting-php-class.html |