Created
May 23, 2010 15:38
-
-
Save navarr/411026 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class User | |
| { | |
| private $core; | |
| private $data; | |
| private $loaded = false; | |
| function __construct(&$core,$uid = null) | |
| { | |
| $this->core = $core; | |
| if(intval($uid)) { $this->load($uid); } | |
| } | |
| function load($id) | |
| { | |
| if(!intval($id)) { return false; } | |
| $r = $this->core->db->query_first("SELECT * FROM `users` WHERE `id`='{$id}'"); | |
| if($r) | |
| { | |
| $this->data = $r; | |
| $this->loaded = true; | |
| return true; | |
| } else { return false; } | |
| } | |
| function __set($var,$val) | |
| { | |
| $this->data[$var] = $val; | |
| if($this->loaded) | |
| { | |
| $sql_var = $this->core->db->escape_string($var); | |
| $sql_val = $this->core->db->escape_string($val); | |
| $this->core->db->query("UPDATE `users` SET `{$var}`=`{$val}` WHERE `id`={$this->data['id']}"); | |
| } | |
| } | |
| function save() | |
| { | |
| $updatestr = ""; | |
| $col_ra = array(); | |
| $val_ra = array(); | |
| foreach($this->data as $key => $val) | |
| { | |
| $key = $db->escape_string($key); | |
| $val = $db->escape_string($val); | |
| $col_ra[] = $key; | |
| $val_ra[] = $key; | |
| $updatestr .= "`{$key}`='{$val}',"; | |
| } | |
| $updatestr = substr($updatestr,0,strlen($updatestr)-1); | |
| if($this->data['id']) | |
| { | |
| $this->core->db->query("UPDATE `users` SET {$updatestr} WHERE `id`={$this->data['id']}"); | |
| } else { | |
| $cols = "'".explode("','",$col_ra)."'"; | |
| $vals = "'".explode("','",$val_ra)."'"; | |
| $this->core->db->query("INSERT INTO `users` ({$cols}) VALUES ({$vals})"); | |
| $this->data["id"] = $this->core->db->insert_id(); | |
| } | |
| } | |
| function &__get($var) | |
| { | |
| return $this->data[$var]; | |
| } | |
| function update() | |
| { | |
| $previous_data = $this->data; | |
| if($this->load($this->data['id'])) | |
| { if($previous_data === $this->data) { return false; } else { return true; } } | |
| else | |
| { return false; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment