11. maj 2007 - 13:24
Der er
1 kommentar
Klasse der kan insætte og opdatere
Jeg har lavet en klasse som jeg burger til at indsætte I tabeller jeg vil meget gerne videreudvikle den klasse så jeg også kan bruge den til at opdatere eksisterende rows.
Jeg vil gerne have hjælp til hvordan jeg gør dette på den smarteste måde
Her er koden som den ser ud nu
class InsertAndUpdate
{
public $registrations_id = null;
public $registrations_name = null;
public $registrations_type = null;
public $registrations_email = null;
public $registrations_phone = 0;
public $workshop_id = 3;
public function insert()
{
mysql_query("INSERT INTO `workshop_registrations` ( `registrations_id` , `registrations_name` , `registrations_type` , `registrations_email` , `registrations_phone` , `workshop_id` )
VALUES ('".mysql_real_escape_string($this->registrations_id)."',
'".mysql_real_escape_string($this->registrations_name)."',
'".mysql_real_escape_string($this->registrations_type)."',
'".mysql_real_escape_string($this->registrations_email)."',
'".mysql_real_escape_string($this->registrations_phone)."',
'".mysql_real_escape_string($this->workshop_id)."')");
}
}
Her er lidt til inspiration. Jeg har ikke testet om det virker. Pricippet er taget fra Python hvor man programmerer "custom" dictionaries (arrays).
/*
* Eksempel med brug af en gæstebog. Tabellen består af:
*
* id Unikt ID (INT NOT NULL)
* author Skribent navn (VARCHAR)
* message Besked (TEXT)
*/
class SqlParser {
private $__data = array();
private $__table = null;
private $__idfield = null;
private $__isnew = true;
public function SqlParser($table, $idfield) {
$this->__table = $table;
$this->__idfield = $idfield;
}
public function set($k, $v) {
$this->__data[$k] = $v;
}
public function get($k) {
return $this->__data[$k];
}
public function update($arr) {
$this->__data = $arr;
}
public function load($v, $k=null) {
$k = ($k) ? $k : $this->__idfield;
$query = "SELECT * FROM ".$this->__table."
WHERE ".$k." = '".$v."' LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
if(!mysql_num_rows($result)) {
return false;
}
$this->__data = mysql_fetch_array($result);
$this->__table = $table;
$this->__isnew = false;
return $this->__data;
}
public function write() {
if(!count(array_keys($this->__data))) {
return false;
}
if($this->__isnew) {
$_keys = array();
$_values = array();
foreach($this->__data as $k => $v) {
array_push($_keys, $k);
array_push($_values, "'.$v.'");
}
$query = "INSERT INTO ".$this->__table."
(".implode(',', $_keys).")
VALUES (".implode(',', $_values).")";
} else {
$_items = array();
foreach($this->__data as $k => $v) {
array_push($_items, ( $k." = '".$v."'"));
}
$query = "UPDATE ".$this->__table."
SET (".implode(',', $_items).")
WHERE ".$this->__idfield." = '".$this->__idvalue."'";
}
mysql_query($query) or die(mysql_error());
}
}
// EKSEMPEL 1: OPRET NY BESKED
$item = SqlParser('gaestebog', 'id');
$item->set('author', 'Mikael Nielsen');
$item->set('message', 'Hej, fed gæstebog');
$item->write();
// EKSEMPEL 2: ÅBEN BESKED, REDIGER OG GEM
$item = SqlParser('gaestebog', 'id');
$item->load(12);
$item->set('author', 'Peter');
$item->set('message', 'Navn rettet til peter');
$item->write();