<?php
require_once "BaseDB.class.php";
// ログインをするクラス
class Topic extends BaseDB
{
// 次章では、ここ以下を Settings.class.php へ以降。
// ここより 上は、BaseDB.class.php とし、各DBアクセスの基本クラスとする。
//指定されたtopicのIDから記事を一件取得
public function loadTopic( $topicid='' )
{
$qstring = "select topicid,dt,lastupdate,fdelete,catid," .
"title,body from b_webdiary where topicid=$topicid";
//クエリを実行
$qresult = mysql_query($qstring);
if(!$qresult){
$this->errorm = "DB Error:[$qstring]";
$this->errors++;
return false;
}
if(!$fresult = mysql_fetch_array($qresult))
{
$this->errorm = "日記情報がありません。";
$this->errors++;
return false;
}
$this->topicid = $fresult['topicid'];
$this->dt = $fresult['dt'];
$this->lastupdate = $fresult['lastupdate'];
$this->fdelete = $fresult['fdelete'];
$this->catid = $fresult['catid'];
$this->title = $fresult['title'];
$this->body = $fresult['body'];
return true;
}
public function saveTopic( $id='' )
{
if( $this->fdelete == '' ){ $delf = 0; } else { $delf = 1; }
if( $this->topicid == 0 ){
$qstring = "insert into b_webdiary (lastupdate,dt,fdelete,catid,title,body)values(" .
"NOW(),NOW(),$delf,$this->catid,'$this->title','$this->body')";
} else {
$qstring = "update b_webdiary set ".
"lastupdate = NOW(), ".
"fdelete = $delf, ".
"catid = $this->catid, ".
"title = '$this->title', ".
"body = '$this->body' ".
" where topicid=$this->topicid";
}
//クエリを実行
$qresult = mysql_query($qstring);
if(!$qresult){
$this->errorm = "DB Error:[$qstring]";
$this->errors++;
return false;
}
return true;
}
public function getTopicsAll( $sql )
{
$qstring = "select d.topicid,DATE_FORMAT( d.dt,'%Y/%m/%d %T') as postdatef,d.lastupdate,d.fdelete," .
"d.catid,b.category,d.title,d.body " .
" from b_webdiary d left join b_categories b " .
" on d.catid = b.catid ".
$sql.
"order by d.topicid DESC limit 41";
//クエリを実行
$qresult = mysql_query($qstring);
if(!$qresult){
$this->errorm = "DB Error:[$qstring]";
$this->errors++;
return false;
}
$arr = $this->mysql_fetch_all( $qresult );
return $arr;
}
public function deleteTopics()
{
$qstring = "delete from b_webdiary where fdelete = 1";
//クエリを実行
$qresult = mysql_query($qstring);
if(!$qresult){
$this->errorm = "DB Error:[$qstring]";
$this->errors++;
return false;
}
return true;
}
public function getTopicsCategory( $catid )
{
$qstring = "select d.topicid,DATE_FORMAT( d.dt,'%Y/%m/%d') as postdatef,DATE_FORMAT( d.dt,'%H:%i') as posttimef,d.lastupdate,d.fdelete," .
"d.catid,b.category,d.title,d.body " .
" from b_webdiary d left join b_categories b " .
" on d.catid = b.catid where d.catid=$catid order by d.dt desc";
//クエリを実行
$qresult = mysql_query($qstring);
if(!$qresult){
$this->errorm = "DB Error:[$qstring]";
$this->errors++;
echo( $this->errorm );
return false;
}
$all = array();
while( $rec = mysql_fetch_assoc($qresult) ){
$rec['bodyf'] = ereg_replace( "\n","<br>", $rec['body'] );
$rec['bodyf'] = ereg_replace( "<br><br>","</p>\n<p>", $rec['bodyf'] );
$rec['bodyf'] = '<p>' . $rec['bodyf'] . '</p>';
$rec['commentcount'] = $this->getCommentCnt( $rec['topicid'] );
$all[] = $rec;
}
return $all;
}
public function getTopics( $linecount )
{
$qstring = "select d.topicid,DATE_FORMAT( d.dt,'%Y/%m/%d') as postdatef,DATE_FORMAT( d.dt,'%H:%i') as posttimef,d.lastupdate,d.fdelete," .
"d.catid,b.category,d.title,d.body " .
" from b_webdiary d left join b_categories b " .
" on d.catid = b.catid order by d.topicid desc";
//クエリを実行
$qresult = mysql_query($qstring);
if(!$qresult){
$this->errorm = "DB Error:[$qstring]";
$this->errors++;
return false;
}
$all = array();
for ($i=0; $i<$linecount; $i++ ) {
if( $rec = mysql_fetch_assoc($qresult) ){
$rec['bodyf'] = ereg_replace( "\n","<br>", $rec['body'] );
$rec['bodyf'] = ereg_replace( "<br><br>","</p>\n<p>", $rec['bodyf'] );
$rec['bodyf'] = '<p>' . $rec['bodyf'] . '</p>';
$rec['commentcount'] = $this->getCommentCnt( $rec['topicid'] );
$all[] = $rec;
}
}
return $all;
}
public function getTopicsSearch( $keywd )
{
$qstring = "select d.topicid,DATE_FORMAT( d.dt,'%Y/%m/%d') as postdatef,DATE_FORMAT( d.dt,'%H:%i') as posttimef,d.lastupdate,d.fdelete," .
"d.catid,b.category,d.title,d.body " .
" from b_webdiary d left join b_categories b " .
" on d.catid = b.catid where d.title regexp '$keywd' or d.body regexp '$keywd' order by d.dt desc";
//クエリを実行
$qresult = mysql_query($qstring);
if(!$qresult){
$this->errorm = "DB Error:[$qstring]";
$this->errors++;
echo( $this->errorm );
return false;
}
$all = array();
while( $rec = mysql_fetch_assoc($qresult) ){
$rec['bodyf'] = ereg_replace( "\n","<br>", $rec['body'] );
$rec['bodyf'] = ereg_replace( "<br><br>","</p>\n<p>", $rec['bodyf'] );
$rec['bodyf'] = '<p>' . $rec['bodyf'] . '</p>';
$rec['commentcount'] = $this->getCommentCnt( $rec['topicid'] );
$all[] = $rec;
}
return $all;
}
public function getCommentCnt( $topicid )
{
$qstring = "select count(*) as cnt from b_comments where topicid=$topicid";
//クエリを実行
$qresult = mysql_query($qstring);
if(!$qresult){
$this->errorm = "DB Error:[$qstring]";
$this->errors++;
return false;
}
$rec = mysql_fetch_assoc($qresult);
return $rec['cnt'];
}
// プロパティ参照
function __get( $property )
{
return $this->$property;
}
// プロパティセット
function __set( $property, $value )
{
$this->$property = $value;
}
} //クラス終了
?>
0 件のコメント:
コメントを投稿