Witam,
wykonałem taką klasę do obsługi DB:
class db{
protected $query;
protected $no = 4;
protected $limit;
protected $table;
protected $offset;
protected $columns;
protected $group;
protected $where = "";
protected $order;
protected $columns_query;
private $connection;
public function __construct(){
$this->columns_query = array(); $this->query = "SELECT * FROM ".$this->table." ";
foreach($this->columns AS $col){
$this->columns_query[] = $col[0];
}
}
public function __destruct(){
}
public function getColumns($value){
$query = "SELECT ";
$order = "";
$this->columns_query = array(); foreach($value AS $col){
$query .= $col.", ";
$this->columns_query[] = $col;
}
$query = substr($query, 0
, -2
); $query .= " FROM ".$this->table." ";
$this->query = $query;
return $this;
}
private function columnExist($start, $name){
$find = false;
for($q=0; $q<$this->no; $q++){
$find = true;
}
}
return $find;
}
public function __call($name, $value){
if(substr($name, 0, 8)=="filterBy"){ $col = $this->columnExist(8, $name);
if($col!=1){
$this->where .= $col."='".$value[0]."' AND ";
}else{
}
}else if(substr($name, 0, 7)=="orderBy"){ $col = $this->columnExist(7, $name);
if($col!=1){
$this->order = "ORDER BY ".$col." ".strtoupper($value[0
]); }else{
}
}else if($name=="setLimit"){
$this->limit = $value[0];
}else if($name=="setOffset"){
$this->offset = $value[0];
}else if(substr($name, 0, 7)=="groupBy"){ $col = $this->columnExist(7, $name);
if($col!=1){
$this->group = $col;
}
}
return $this;
}
public function where($where){
$this->query .= "(".$where.") AND ";
return $this;
}
public function find(){
$this->where = substr($this->where, 0
, -5
); $this->query .= "WHERE ".$this->where." ";
}
if($this->group!=NULL){
$this->query .= "GROUP BY ".$this->group." ";
}
$this->query .= $this->order." ";
if($this->offset!=NULL || $this->limit!=NULL){
$this->query .= "LIMIT ";
if($this->offset!=NULL){
$this->query .= $this->offset.",";
}else{
$this->query .= "0,";
}
if($this->limit!=NULL){
$this->query .= $this->limit;
}else{
$this->query .= "0";
}
}
echo "Wykonuję zapytanie o treści:<br />".$this->query."<br />";
$q = 0;
foreach($this->columns_query AS $col){
$return[$q][$col] = $row[$col];
}
$q++;
}
return $return;
}
}
Każda tabela z bazy danych musi być reprezentowana, przez jedną klasę, dziedziczącą po powyższej klasie. Przykładowo:
class db_uzytkownicy extends db{
protected $no = 4; // ilość kolumn w tabeli
protected $table = "uzytkownicy"; // nazwa tabeli w bazie danych
// spis kolumn w tabeli
protected
$columns = array( array('id', 'INT', 10), // nazwa, typ, długość array('imie', 'VARCHAR', 255
), array('email', 'VARCHAR', 255
), array('nazwisko', 'VARCHAR', 255
), );
}
Jak oceniacie powyższy kod? Proszę o surowe i konstruktywne opinie: co poprawić, co polepszyć, co usunąć?
Pozdrawiam (IMG:
style_emoticons/default/smile.gif)