ruạṛ
<?php /** * list_option object * contains list_option objects */ class list_option { /** * define variables matched to to database fields * may not have identical names, see $this->load_from_data for conversions */ var $option_id , $list_id , $text , $active , $position ; /** * flags * @var bool $update */ var $update = false; /** * object construction function * @param int $id unique database record id */ function __construct($id=0) { /* * set minimum visibility */ $this->option_id = 0; $this->list_id = 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 $category mysql dataset */ $record = mysql_query("select * from list_options where option_id = '$id' "); if($record && mysql_num_rows($record)>0) { $this->load_from_data(mysql_fetch_assoc($record)); mysql_free_result($record); // clean up } return; } /** * populate object - can be done on init or manually * @param array $d data; */ function load_from_data($d=array()) { if(!empty($d)) { $this->option_id = $d['option_id']; $this->list_id = $d['list_id']; $this->text = $d['option_text']; $this->active = $d['active']; $this->position = $d['position']; } return; } /** * add record to the database * @return bool */ function create() { global $message; /** * option data is set by containing option_list */ /** * validate required fields */ $m = ''; if(!is_numeric_id($this->list_id,0)){ $m .= 'Please enter a valid list id for all list options <br />'; } if($this->text==''){ $m .= 'Please enter text for all list options <br />'; } if(select_one('list_options','option_id',"concat_ws(':',list_id,option_text)",$this->list_id.':'.$this->text)) { $m .= 'That option already exists <br />'; } if($m != '') { $message .= $m; return false; } /* * defaults if not supplied */ if(!$this->position) { $this->position = select_one('list_options','max(position)','list_id',$this->list_id) + 10; } /* * sql query */ $fields = 'list_id , option_text , position , active '; $values = "'$this->list_id' , '$this->text' , '$this->position' , '$this->active' "; $query = "insert into list_options ( $fields ) values ( $values )"; if(!mysql_query($query)) { $message .= mysql_error(); return false; } //else $this->option_id = mysql_insert_id(); $message .= 'Option record created <br />'; return true; } /** * update record in the database * @param bool $silent suppress success message @see options_list->update_all_options() * @return bool */ function update($silent=false) { global $message; /** * san check */ if(!$this->option_id) { $message .= 'Invalid option <br />'; return false; } /** * option data and update flag is set by containing option_list */ if(!$this->update) { return false; } /** * validate required fields */ $m = ''; if(!is_numeric_id($this->list_id,0)){ $m .= 'Please enter a valid list id for all list options <br />'; } if($this->text==''){ $m .= 'Please enter text for all list options <br />'; } if($m != '') { $message .= $m; return false; } /** * @var string $query update query */ $query = "update list_options set option_text = '$this->text' , position = '$this->position' , active = '$this->active' where option_id = '$this->option_id'"; $update = mysql_query($query); if(!$update) { $message .= mysql_error(); return false; } $message .= ($silent) ? '' : 'Option record updated <br />'; return true; } /* * remove all-the-things * @return bool */ function delete() { global $message; /** * san check */ if(!is_numeric_id($this->option_id,0)) { $message .= 'Unable to delete list option: invalid id <br />'; return false; } /** * remove (delete) from database */ return $this->remove_from_database(); } /** * remove record from database * @return bool */ function remove_from_database() { global $message; /** * san check */ if(!is_numeric_id($this->option_id,0)) { $message .= 'Unable to remove option from database: invalid id <br />'; return false; } /** * delete list record * @var string $query delete query * @return bool */ $query = "delete from list_options where option_id = '$this->option_id'"; $result = mysql_query($query); if(!$result || mysql_affected_rows() != 1) { $message .= 'There was an error '.mysql_error().'<br />Affected rows: '.mysql_affected_rows(); return false; } // $message .= $this->text.' database record deleted <br />'; return true; } } ?>
cải xoăn