Class table in php language
<?php
/**
* Description of Duongdong
*
* @author Duong Dong Doan
*/
class Duongdong { // Generalation
private $host;
private $user;
private $pass;
private $db;
private $table;
public $con;
public $num=0; //number field of table
public $once; //one record, array type
public $field;
// public function __construct(){
// $this->host=\"localhost\";
// $this->user=\"root\";
// $this->pass=\"\";
// }
public function __construct($database,$table){ //On local
$this->host=\"localhost\";
$this->user=\"root\";
$this->pass=\"\";
$this->db=$database;
$this->table=$table;
$this->field=array();
}
// public function __construct($host,$user,$pass,$database,$table){ // On server
// $this->host=$host;
// $this->user=$user;
// $this->pass=$pass;
// $this->db=$database;
// $this->table=$table;
// }
public function connect(){ //Connect host and select database
$con=mysql_connect(\"{$this->host}\",\"{$this->user}\",\"{$this->pass}\");
mysql_select_db($this->db,$con);
return $con;
}
function showtable(){ //Show a table
self::connect();
$query=\"select * from {$this->table}\";
$result= mysql_query($query);
$this->num= mysql_num_fields($result);
$row= mysql_fetch_array($result);
echo \"<form name=frm id=frm method=post >\";
echo \"<table border=1>\";
echo \"<th><input type=checkbox name=\'cka\' id=\'cka\' title=\'Select All\' onclick=checkall() /></th>\";
for($a=0;$a<$this->num;$a++){
$ele=each($row);
$ele=each($row);
echo \"<th>\";
echo $ele[\"key\"];
echo \"</th>\";
$this->field[$a]=$ele[\"key\"];
}
echo \"<th colspan=2>Thao tác</th>\";
echo \"<tr>\";
echo \"<td><input type=checkbox name=ck[] value={$row[0]} onclick=\'cka.checked=false\' /></td>\";
for($w=0;$w<$this->num;$w++){
echo \"<td>\";
echo $row[$w];
echo \"</td>\";
}
echo \"<td><a href=?edit=$row[0]><img src=edit.png title=\'Edit $row[1]\'></a></td>\";
echo \"<td><a href=?del=$row[0]><img src=del.png title=\'Delete $row[1]\'></a></td>\";
echo \"</tr>\";
while($row= mysql_fetch_array($result)){
echo \"<tr>\";
echo \"<td><input type=checkbox name=ck[] value={$row[0]} onclick=\'cka.checked=false\' /></td>\";
for($w=0;$w<$this->num;$w++){
echo \"<td>\";
echo $row[$w];
echo \"</td>\";
}
echo \"<td><a href=?edit=$row[0]><img src=edit.png title=\'Edit $row[1]\'></a></td>\";
echo \"<td><a href=?del=$row[0]><img src=del.png title=\'Delete $row[1]\'</a></td>\";
echo \"</tr>\";
}
echo \"</table>\";
echo \"<a href=?add=addnew>Thêm mới</a>\";
echo \"<input type=submit value=DeleteItems name=delitems />\";
echo \"</form>\";
}
function showtable_con($id){
self::connect();
$query=\"select * from {$this->table} where \".$this->field[0].\" = \'\".$id.\"\' \";
$result= mysql_query($query);
$this->num= mysql_num_fields($result);
$row= mysql_fetch_array($result);
echo \"<form name=frm id=frm method=post >\";
echo \"<table border=1>\";
echo \"<th><input type=checkbox name=\'cka\' id=\'cka\' title=\'Select All\' onclick=checkall() /></th>\";
for($a=0;$a<$this->num;$a++){
$ele=each($row);
$ele=each($row);
echo \"<th>\";
echo $ele[\"key\"];
echo \"</th>\";
$this->field[$a]=$ele[\"key\"];
}
echo \"<th colspan=2>Thao tác</th>\";
echo \"<tr>\";
echo \"<td><input type=checkbox name=ck[] value={$row[0]} onclick=\'cka.checked=false\' /></td>\";
for($w=0;$w<$this->num;$w++){
echo \"<td>\";
echo $row[$w];
echo \"</td>\";
}
echo \"<td><a href=?edit=$row[0]><img src=edit.png title=\'Edit $row[1]\'></a></td>\";
echo \"<td><a href=?del=$row[0]><img src=del.png title=\'Delete $row[1]\'></a></td>\";
echo \"</tr>\";
while($row= mysql_fetch_array($result)){
echo \"<tr>\";
echo \"<td><input type=checkbox name=ck[] value={$row[0]} onclick=\'cka.checked=false\' /></td>\";
for($w=0;$w<$this->num;$w++){
echo \"<td>\";
echo $row[$w];
echo \"</td>\";
}
echo \"<td><a href=?edit=$row[0]><img src=edit.png title=\'Edit $row[1]\'></a></td>\";
echo \"<td><a href=?del=$row[0]><img src=del.png title=\'Delete $row[1]\'</a></td>\";
echo \"</tr>\";
}
echo \"</table>\";
echo \"<a href=?add=addnew>Thêm mới</a>\";
echo \"<input type=submit value=DeleteItems name=delitems />\";
echo \"</form>\";
}
function insert($arrayvalue){ // Insert new record
self::connect();
$value=\'\\\'\'.join($arrayvalue,\"\',\'\").\'\\\'\';
$query=\"insert into {$this->table} values(\".$value.\")\";
$result=mysql_query($query);
return $result;
}
function dropitem($id){ //delete a item
self::connect();
$query=\"delete from {$this->table} where {$this->field[0]}=\'$id\'\";
echo $query;
$result=mysql_query($query);
return $result;
}
function dropitems($array){ // Delete multiple items
$items=\'\\\'\'.join($array,\"\',\'\").\'\\\'\';
$query=\"delete from \".$this->table.\" where \".$this->field[0].\" IN (\".$items.\")\";
$result=mysql_query($query);
return $result;
}
function edit($array){ //update record
$i=0;
$AB=array();
$ele= each($array);
$dk=$this->field[0].\"=\'\".$ele[\"value\"].\"\'\";
while ($ele= each($array)){
$i++;
$AB[$i]=$this->field[$i].\"=\'\".$ele[\"value\"].\"\'\";
}
$set=join($AB, \' ,\');
$query=\"update \".$this->table.\" set \".$set.\" where \".$dk;
$result=mysql_query($query);
return $result;
}
function once($id){ // once record with id=$id
$this->once=array();
$query=\"select * from $this->table where {$this->field[0]}=\'$id\'\";
$result=mysql_query($query);
$this->once=mysql_fetch_array($result);
return $this->once;
}
}
?>
Bạn đang đọc truyện trên: AzTruyen.Top