这是我第一次使用 OO PHP,我创建了一个 Person 类,我需要通过查询数据库或 POST 值来创建 Person 对象。然后我需要将数据保存到数据库。
这是我的代码,我不知道这是否是正确的方法。我需要一些建议。
class Persona {
protected $id=NULL;
protected $nome;
protected $cognome;
protected $cf=NULL;
protected $indirizzo=NULL;
protected $civico=NULL;
protected $citta=NULL;
protected $cap=NULL;
protected $provincia=NULL;
protected $nazione=NULL;
protected $telefono=NULL;
protected $fax=NULL;
protected $cellulare=NULL;
protected $email;
protected $data_registrazione;
protected $tipo_registrazione;
public function createPersona($postData=NULL,$id=NULL,$email=NULL)
{
global $_CONFIG;
if(is_array($postData) && isset($postData['nome']) && isset($postData['cognome']) && isset($postData['email']) && isset($postData['tipo_registrazione']))
{
$record=$postData;
}elseif(isset($id)){
$result=mysql_query("SELECT * FROM ".$_CONFIG['tbl_persone']." WHERE id='".escape_string($id)."'");
if(mysql_num_rows($result)!=1) return false;
$record = mysql_fetch_assoc($result);
}elseif(isset($email)){
$result=mysql_query("SELECT * FROM ".$_CONFIG['tbl_persone']." WHERE email='".strtolower(escape_string($email))."'");
if(mysql_num_rows($result)!=1) return false;
$record = mysql_fetch_assoc($result);
}else{
return false;
}
if(isset($record['cf'])) $record['cf']=strtoupper($record['cf']);
if(isset($record['cap'])) $record['cap']=strtoupper($record['cap']);
if(!isset($record['nazione']) && isset($record['prefisso'])) $record['nazione']=$record['prefisso'];
$record['email']=strtolower($record['email']);
if(!isset($record['data_registrazione'])) $record['data_registrazione']=date('Y-m-d H:i:s');
$vars=get_object_vars($this);
foreach($vars as $key=>$value)
{
if(isset($record[$key])){$this->$key=$record[$key];}
}
if(!$this->validatePersona())return false;
return true;
}
protected function validatePersona()
{
if(isset($this->id) && !validateID($this->id)) return false;
if(isset($this->cf) && !validateCF($this->cf)) return false;
if(isset($this->cap) && !validateCAP($this->cap)) return false;
if(isset($this->email) && !validateEmail($this->email)) return false;
return true;
}
public function savePersona()
{
global $_CONFIG;
$vars=get_object_vars($this);
foreach($vars as $key=>$value)
{
if($key!='id')
{
if(isset($this->$key))
{
$columns.=$key.",";
$values.="'".escape_string($this->$key)."',";
}
}
}
if(!mysql_query("INSERT INTO ".$_CONFIG['tbl_persone']." (".substr($columns,0,-1).") VALUES (".substr($values,0,-1).")"))
{
return false;
}else{
return true;
}
}
}
$p=new Persona();
if(!$p->createPersona($_POST)){
echo 'Si è verificato un errore.<br />Riprova più tardi. [0]';
exit;
}
if($p->createPersona(NULL,NULL,$_POST['email'])){
echo 'Indirizzo email già registrato.';
exit;
}
if(!$p->savePersona()){
echo 'Si è verificato un errore.<br />Riprova più tardi. [2]';
exit;
}
第二步是使用我的数据库中的人员数据创建一个动态 HTML 表,现在通过程序语言获取数据库并创建一个数组,然后使用 foreach 构造打印该表,但我不知道如何使用 OO 语言.
谢谢你们
弗朗切斯科