コードを実行すると、このエラーが返されます
警告: PDOStatement::fetchAll() [pdostatement.fetchall]: SQLSTATE[HY000]: 一般エラー: C:\AppServ\www\cms_project\lib\database_model.class.php の 28 行目に不要な追加パラメータがあります
警告: array_shift() [function.array-shift]: 引数は C:\AppServ\www\cms_project\lib\database_model.class.php の 34 行目の配列である必要があります
警告: C:\AppServ\www\cms_project\lib\cat.class.php の 43 行目の foreach() に無効な引数が指定されました
これは database_model クラスです:
<?php
class database_model
{
public function attribute()
{
$string = array();
foreach($this->db_feild as $feild){
if(is_int($this->$feild) || is_double($this->$feild)){
$string[] = $feild . " = ".$this->$feild."";
}else{
$string[] = $feild ." = '".$this->$feild ."'";
}
}
return implode(', ',$string); // convert array to string
}
public static function read($sql, $type = PDO::FETCH_ASSOC,$class = null)
{
global $db_connect;
$resualt= $db_connect->query($sql); // query from db
if($resualt){
if(null !== $class && $type == PDO::FETCH_ASSOC){
$data = $resualt->fetchAll($type,$class);
}else{
$data = $resualt->fetchAll($type);
}
if(count($data)== 1){
$data = array_shift($data); // array_shift علشان نطلع من الارارى
}
return $data;
}else{
return false;
}
}
private function add()
{
global $db_connect; // to use the conection to database
$sql = "INSERT INTO ".$this->table_name . " SET " .$this->attribute();
$affectedrows = $db_connect->exec($sql); // to include to database
if($affectedrows != false){
$this->id = $db_connect->lastInsertId();
}else{
return false;
}
return true;
}
private function update()
{
global $db_connect;
$sql = "UPDATE ".$this->table_name . " SET " .$this->attribute()
."WHERE id = ".$this->id;
$affectedrows = $db_connect->exec($sql);
return $affectedrows != false ? true : false;
}
public function delete()
{
global $db_connect;
$sql = "DELETE FROM ".$this->table_name."WHERE id = ".$this->id;
$affectedrows = $db_connect->exec($sql);
return $affectedrows != false ? true : false;
}
public function save()
{
return ($this->id === null)? $this->add():$this->update();
}
}
?>
これは猫のクラスです:
class cat extends database_model
{
public $id;
public $user_id;
public $created;
public $title;
public $slug;
public $content;
public $table_name = 'cat';
public $db_feild = array(
'user_id',
'created',
'title',
'slug',
'content',
);
public static function control()
{
// __CLASS__ ==> RETURN THE THIS CLASS
$all_categries = self::read("SELECT * FROM cat ",PDO::FETCH_ASSOC,__CLASS__);
$table = '<table class="cpanel_cat">';
$table .= '<tr>
<th># </th>
<th>category name </th>
<th colspan="2"> control </th>
' ;
if($all_categries !== false){
if(is_object($all_categries)){
$table .= '<tr>
<td>1 </td>
<td>'. $all_categries->title .'</td>
<td><a href="" >EDIT</a> | <a href="" > Delete</a> </td>
';
}else{
$i = 1;
foreach($all_categries as $cat)
$table .= '<tr>
<td>'.$i++ .' </td>
<td>'.$cat->title .'</td>
<td><a href="" >EDIT</a> | <a href="" > Delete</a> </td>
';
}
}else{
$table .= '<tr><td colspan="4">sorry categroy not found !!</td></tr>';
}
$table .='</table>';
return $table;
}
}