class Item { protected $db; // Ugh, database link protected $tb_main; // The users table, duh protected $tb_meta; // The users meta table, jeez protected $valid; // Do we have valid user data? Used for saving and loading meta public $data; // This is where we store everything function __construct($db, $query = false, $load_meta = false, $tb_main, $tb_meta) { $this->db = $db; $this->tb_main = $tb_main; $this->tb_meta = $tb_meta; $this->valid = 0; if ($id) $this->load($query, $load_meta); } function __destruct() { } // Overloading public function __set($name, $value) { $this->data[$name] = $value; } public function __get($name) { if (array_key_exists($name, $this->data)) { return $this->data[$name]; } $trace = debug_backtrace(); trigger_error( 'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE); return null; } // I don't think there'll be much trouble here. We just load in a user (w/ or w/out meta). $query can be either a username or a user id. function load($query, $load_meta = false) { if (is_numeric($query)) $sql = "SELECT * FROM `{$this->tb_main}` WHERE `id` = {$query} LIMIT 1"; else $sql = "SELECT * FROM `{$this->tb_main}` WHERE {$query} LIMIT 1"; $rs = mysql_query($sql, $this->db); if (!$rs || @mysql_num_rows($rs) == 0) return false; $this->clear(); $row = mysql_fetch_assoc($rs); $this->data = $row; $this->valid = 1; if ($load_meta) return $this->loadMeta(); return true; } // Load the meta for the current user function loadMeta() { if ($this->valid) { $sql = "SELECT * FROM `{$this->tb_meta}` WHERE `id` = {$this->data["id"]} LIMIT 1"; $rs = mysql_query($sql, $this->db); if (!$rs || @mysql_num_rows($rs) == 0) return false; $row = mysql_fetch_assoc($rs); foreach ($row as $key => $value) $this->data["meta_$key"] = $value; $this->valid = 2; return true; } else return false; } function save($save_meta = false) { if ($this->valid) { $id = $this->data["id"]; $query = ""; foreach($this->data as $key => $value) { if (substr($key, 0, 5) != "meta_" && $key != "id") { if (!is_numeric($value)) $value = "'{$value}'"; $query .= "`{$key}` = {$value}, "; } } $query = rtrim($query, " ,"); $sql = "UPDATE `{$this->tb_main}` SET {$query} WHERE `id` = {$id} LIMIT 1"; $return = mysql_query($sql, $this->db); if ($save_meta) return $this->saveMeta(); else return $return; } return false; } function saveMeta() { if ($this->valid == 2) { $id = $this->data["id"]; $query = ""; foreach($this->data as $key => $value) { if ($key != "id" && substr($key, 0,5) == "meta_") { $key = substr($key, 5); if (!is_numeric($value)) $value = "'{$value}'"; $query .= "`{$key}` = {$value}, "; } } $query = rtrim($query, " ,"); $sql = "UPDATE `{$this->tb_meta}` SET {$query} WHERE `id` = {$id} LIMIT 1"; return mysql_query($sql, $this->db); } return false; } function insert($data, $meta = false, $load = false) { if (is_array($data)) { $fields_sql = $values_sql = ""; foreach($data as $key => $value) { $fields_sql .= ",`{$key}`"; $values_sql .= is_numeric($value) ? ",{$value}" : ",'{$value}'"; $this->data[$key] = $value; } $meta_fields_sql = $meta_values_sql = ""; if (is_array($meta)) { foreach($meta as $key => $value) { $meta_fields_sql .= ",`{$key}`"; $meta_values_sql .= is_numeric($value) ? ",{$value}" : ",'{$value}'"; $this->data["meta_".$key] = $value; } } $sql = "INSERT INTO `{$this->tb_main}` (`id` {$fields_sql}) VALUES (0 {$values_sql})"; if (!mysql_query($sql, $this->db)) return false; $id = mysql_insert_id($this->db); $sql = "INSERT INTO `{$this->tb_meta}` (`id` {$meta_fields_sql}) VALUES ({$id} {$meta_values_sql})"; if (!mysql_query($sql, $this->db)) return false; $this->data["id"] = $id; $this->data["meta_id"] = $id; if ($load) return $this->load($id, is_array($meta)); else return true; } return false; } function validate($valid) { $this->valid = $valid; } function clear() { $this->data = array(); $this->valid = false; } } class ItemSet implements Iterator { public $items; // This is an array for Item objects (it hopefully will be after a load()) protected $tb_main; // See the User class for more info protected $tb_meta; // Same as above protected $db; // Same as above, duh! :\ // Constructor, no problems here. function __construct($db, $condition = false, $load_meta = false, $tb_main, $tb_meta) { $this->items = array(); $this->db = $db; $this->tb_main = $tb_main; $this->tb_meta = $tb_meta; if ($condition) $this->load($condition, $load_meta); } function __destruct() { } // Iterator functions public function rewind() { reset($this->items); } public function current() { return current($this->items); } public function key() { return key($this->items); } public function next() { return next($this->items); } public function valid() { return $this->current() !== false; } function load($condition, $load_meta = false) { $sql = "SELECT * FROM `{$this->tb_main}` WHERE {$condition}"; $rs = mysql_query($sql, $this->db); if (!$rs || @mysql_num_rows($rs) == 0) return false; $this->clear(); while($row = mysql_fetch_assoc($rs)) { $temp_item = new Item($this->db, false, false, $this->tb_main, $this->tb_meta); $temp_item->data = $row; $this->items[$row["id"]] = $temp_item; $this->items[$row["id"]]->validate(1); } if ($load_meta) return $this->loadMeta(); return true; } function loadMeta() { $loaded = ""; foreach($this->items as $key => $value) $loaded .= $key . ","; $loaded = rtrim($loaded, ","); $sql = "SELECT * FROM `{$this->tb_meta}` WHERE `id` IN({$loaded})"; $rs = mysql_query($sql, $this->db); if (!$rs || @mysql_num_rows($rs) == 0) return false; while ($row = mysql_fetch_assoc($rs)) { foreach ($row as $key => $value) $this->items[$row["id"]]->data["meta_$key"] = $value; $this->items[$row["id"]]->validate(2); } return true; } function clear() { $this->items = array(); } }