FrontPage
Categories.class.phpのソースコード
<?php
require_once("MySmarty.class.php");
require_once "BaseDB.class.php";
// カテゴリを扱うクラス
class Categories extends BaseDB
{
// カテゴリを返す
function getCategory( $catid = 0 ) {
$sqlstr = "select * from b_categories order by catid asc";
if( $catid != 0 )
$sqlstr = $sqlstr." where catid='$catid'";
$result = @mysql_query( $sqlstr );
if( $result == FALSE ){
throw new exception( "カテゴリテーブルにアクセスできません" );
}
if( $catid != 0 ) {
if( @mysql_num_rows($result) == 0 )
throw new exception( "カテゴリIDに対応するカテゴリは存在しません" );
$arr = mysql_fetch_array( $result );
return $arr['category'];
}
else {
$arr = $this->mysql_fetch_all( $result );
return $arr;
}
}
public function makeSelect( $selected = '' )
{
$smarty = new MySmarty();
try {
$arr = $this->getCategory();
$categories_arr = array();
$catid_arr = array();
$selected_arr = array();
foreach( $arr as $key => $value ) {
if( $value['category'] != '' ){
array_push( $categories_arr, $value['category'] );
array_push( $catid_arr , $value['catid'] );
if( $value['catid'] == $selected )
array_push( $selected_arr, "selected" );
else
array_push( $selected_arr, "" );
}
}
$smarty->assign( "categories", $categories_arr );
$smarty->assign( "catid", $catid_arr );
$smarty->assign( "selected", $selected_arr );
return $smarty->fetch("viewcategories.tpl");
}
catch( exception $e ) {
throw $e;
}
}
// 新規カテゴリの追加
function addCategory( $catstr ) {
$sqlstr = "insert into b_categories ( category ) values ('$catstr')";
$result = mysql_query( $sqlstr );
if( $result == FALSE )
throw new exception( "カテゴリの登録に失敗しました" );
try {
return $this->getCategoryID( $catstr );
}
catch( exception $e ) {
throw $e;
}
}
// カテゴリIDを返す
function getCategoryID( $catstr ) {
$sqlstr = "select * from b_categories where category='$catstr'";
$result = @mysql_query( $sqlstr );
if( $result == FALSE ) {
throw new exception( "カテゴリテーブルにアクセスできません" );
}
$arr = @mysql_fetch_array( $result );
return $arr['catid'];
}
// カテゴリの削除
function deleteCategory( $catid ) {
if( $catid <= 1 ) {
$this->errorm = "デフォルトのカテゴリは削除できません";
return;
}
$sqlstr = "delete from b_categories where catid='$catid'";
$result = @mysql_query( $sqlstr );
if( $result == FALSE ){
$this->errorm = "カテゴリ$catidは削除できませんでした";
return;
}
// トピックのアップデートを行う
$sqlstr = "update b_webdiary set catid='1' where catid='$catid'";
$result = @mysql_query( $sqlstr );
if( $result == FALSE ){
$this->errorm = "トピックのカテゴリが修正できませんでした";
return;
}
}
// プロパティ参照
function __get( $property )
{
return $this->$property;
}
// プロパティセット
function __set( $property, $value )
{
$this->$property = $value;
}
} //クラス終了
?>