ruạṛ
<?php /** * stockist_region object * may contain stockist objects */ class stockist_region { /** * define variables matched to to database fields * may not have identical names, see $this->load_from_data for conversions */ var $region_id , $name , $international , $active , $order_id ; /** * subcategory/product containers * @var array $stockists */ var $stockists = array(); /** * object construction function * @param int $id unique database record id */ function __construct($id=0) { /* * set minimum visibility */ $this->region_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 */ $recordset = mysql_query("select * from stockist_regions where region_id = '$id' "); if($recordset && mysql_num_rows($recordset)>0) { $this->load_from_data(mysql_fetch_assoc($recordset)); } mysql_free_result($recordset); // 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->region_id = $d['region_id']; $this->name = $d['region_name']; $this->international = $d['international']; $this->active = $d['active']; $this->order_id = $d['order_id']; } return; } /** * populate/return stockists * var bool $all flag to switch between all/active, by default only load active * @return array */ function load_stockists($all=false) { /** * @var int $id this region id * @var string $query constructed mysql query * @var resource $records mysql_resource set */ $id = $this->region_id; $query = "select * from stockists where region_id = '$id'"; $query .= ($all) ? '' : " and active = '1'"; $query .= ' order by order_id desc'; $records = mysql_query($query); $this->stockists = array(); //empty while($r = mysql_fetch_assoc($records)) { $s = new stockist(); $s->load_from_data($r); array_push($this->stockists, $s); } return $this->stockists; } /** * 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 stockist_regions ( region_name , international , active , order_id ) values ( '$name' , '$this->international' , '$this->active' , '$this->order_id' )"; if(!mysql_query($query)) { //die(mysql_error()); return false; } $this->region_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->region_id; $name = $this->name; if(!$id || $name=='') { return false; } /** * @var string $query update query */ $query = "update stockist_regions set region_name = '$name' , international = '$this->international' , active = '$this->active' , order_id = '$this->order_id' where region_id = '$id' "; $update = mysql_query($query); 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->region_id) { //set_message('Invalid user details'); return false; } /** * delete stockists then region * @var string $query delete query */ $query = "delete from stockists where region_id = '$this->region_id'"; $result = mysql_query($query); $sDel = mysql_affected_rows(); $query = "delete from stockist_regions where region_id = '$this->region_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('Region successfully deleted'); return true; } } ?>
cải xoăn