入力された(投稿された)データの登録部分を作成します。
あらかじめきちんと設計していないのはやはり問題でしょうか。メッセージ表示のときに
作成したクラス(B3Post)の使い勝手が悪すぎます。当初は、表示のことだけを考えていましたが
せっかくですので登録にも使用したいと思います。
<?php class B3Post{ public $subject; public $message; public $user; public $post_date; function __construct($pid,$db){ $sql = "SELECT * FROM post_message WHERE post_id = " . $pid; $dyn = $db->Query($sql); if(!$dyn){ die("query error"); } $row = $db->FetchRow($dyn); $this->subject = $row['post_subject']; $this->message = str_replace("\n","<br>",$row['post_msg']); $this->user = $row['post_user']; $this->post_date = $row['post_date']; } } ?>
このクラスは、コンストラクタでpost_idを元にメッセージを取得して
メンバ変数に格納しています。
これを廃止し、データ取得用のメンバ関数とデータ登録用のメンバ関数を作成します。
<?php class B3Post{ //メンバ変数 public $subject; public $message; public $user; public $post_date; public $fid; public $tid; public $pid; private $db; private $Errors; //メンバ関数 function __construct($db){ //メンバ変数の初期化 $this->subject = ""; $this->message = ""; $this->user = ""; $this->post_date = ""; $this->db = $db; } //post_idを元にメッセージを取得 function GetPostMessage($pid){ $sql = "SELECT * FROM post_message WHERE post_id = " . $pid; $dyn = $this->db->Query($sql); if(!$dyn){ die("query error1"); } $row = $this->db->FetchRow($dyn); $this->subject = $row['post_subject']; $this->message = str_replace("\n","<br>",$row['post_msg']); $this->user = $row['post_user']; $this->post_date = $row['post_date']; } //メンバ変数にセットされた値をデータベースへ反映する function InsertMessage(){ if(isset($this->pid) and $this->pid != ''){//親となるメッセージのpost_idがあるとき $sql="INSERT INTO post_message(topic_id,post_subject,post_msg,post_user,post_date," . "parent_post_id) VALUES (" . $this->tid . ",'" . $this->subject . "','" . $this->message . "','" . $this->user . "',sysdate()," . $this->pid . ")"; $res = $this->db->Query($sql); if(!$res){ $Errors['message'] = "投稿に失敗しました。"; return false; } $sql="UPDATE topic SET reply_count = reply_count + 1,last_post_date = sysdate() " . "WHERE topic_id=" . $this->tid; $res = $this->db->Query($sql); if(!$res){ $Errors['message'] = "トピックの更新に失敗しました。"; return false; } } else{//新規のトピックを作成するとき //New Topic $sql = "INSERT INTO topic(forum_id,last_post_date" . ") VALUES (" . $this->fid . ",now())"; $res = $this->db->Query($sql); if(!$res){ $Errors['message'] = "トピックの登録に失敗しました。"; return false; } $newtid = $this->db->GetCurrentID(); $sql="INSERT INTO post_message(topic_id,post_subject,post_msg,post_user,post_date" . ") VALUES (" . $newtid . ",'" . $this->subject . "','" . $this->message . "','" . $this->user . "',sysdate())"; $res = $this->db->Query($sql); if(!$res){ $Errors['message'] = "投稿に失敗しました。"; return false; } } return true; } } ?>
Smartyでpublicなメンバ変数を表示する方法しか知らないのでメンバ変数はpublicにしています。
他にもこのクラスの中で、メッセージの入力チェックや重複投稿のチェック、禁止タグのチェック
なども行えそうですね。
メンバ関数 InsertMessage のなかで、2つのテーブルを更新しています。
本来ならトランザクションを実行するということも考えられますが、
クリティカルではないのでこのままいきます。
トランザクションに対応するには、テーブルを作成する際 ENGINE=InnoDBとする必要があるようです。
ここまでで、投稿部分は完成です。細かな部分は、まだまだ沢山ありますが表示、投稿はできるようになりました
後は、トピックの表示とメッセージの表示に投稿のスクリプトを呼ぶように変更すれば
フォーラムの完成です。
Copyright 1997-2010 BBB All rights reserved.