ruạṛ
<?php /** * stockist object */ class stockist { /** * define variables matched to to database fields * may not have identical names, see $this->load_from_data for conversions */ var $stockist_id , $region_id , $name , $address , $phone , $website , $map_code , $active , $order_id ; /** * object construction function * @param int $id unique database record id */ function __construct($id=0) { /* * set minimum visibility */ $this->stockist_id = 0; $this->active = 0; /** * if no id supplied simply prepare object to be populated from dataset eg in creation script */ if(!$id) { return; } /** * else get object data * assumes database connection already established at global level * @var resource $records mysql dataset */ $records = mysql_query("select * from stockists where stockist_id = '$id' "); if($records && mysql_num_rows($records)>0) { $this->load_from_data(mysql_fetch_assoc($records)); mysql_free_result($records); // clean up } return; } /** * populate object - can be done on init or manually * @var array $d data; */ function load_from_data($d=array()) { if(!empty($d)) { $this->stockist_id = $d['stockist_id']; $this->region_id = $d['region_id']; $this->name = $d['stockist_name']; $this->address = $d['stockist_address']; $this->phone = $d['stockist_phone']; $this->website = $d['stockist_website']; $this->map_code = $d['stockist_map_code']; $this->active = $d['active']; $this->order_id = $d['order_id']; } return; } /** * add record to the database * @return bool */ function create() { //global $db; /** * validate required fields * @var string $name */ $name = $this->name; if($name=='') { //die('Invalid user data'); return false; } $query = "insert into stockists ( stockist_name , region_id , stockist_address , stockist_phone , stockist_website , stockist_map_code , active , order_id ) values ( '$name' , '$this->region_id' , '$this->address' , '$this->phone' , '$this->website' , '$this->map_code' , '$this->active' , '$this->order_id' )"; if(!mysql_query($query)) { //die(mysql_error()); return false; } $this->stockist_id = mysql_insert_id(); return true; } /** * update record in the database * @param string $password (not stored in object so must be passed by process script) * @global $db database connection * return bool */ function update() { //global $db; /** * validate required fields * @var int $id * @var string $name */ $id = $this->stockist_id; $name = $this->name; if(!$id || $name=='') { die("ID $id; Name $name"); return false; } /** * @var string $query update query */ $query = "update stockists set stockist_name = '$name' , region_id = '$this->region_id' , stockist_address = '$this->address' , stockist_phone = '$this->phone' , stockist_website = '$this->website' , stockist_map_code = '$this->map_code' , active = '$this->active' , order_id = '$this->order_id' where stockist_id = '$id' "; $update = mysql_query($query) or die(mysql_error()); if(!$update) { //set_message(mysql_error()); return false; } //set_message('Record successfully updated'); return true; } /** * remove record from the database * return bool */ function delete() { //global $db; if(!$this->stockist_id) { //set_message('Invalid user details'); return false; } /** * delete stockist * @var string $query delete query */ $query = "delete from stockists where stockist_id = '$this->stockist_id'"; $result = mysql_query($query); if(!$result || mysql_affected_rows() != 1) { //set_message('There was an error '.mysql_error().'<br />Affected rows: '.mysql_affected_rows()); return false; } //set_message('Stockist successfully deleted'); return true; } } ?>
cải xoăn