id = $params['id']; $this->title = $params['title']; $this->desc = $params['desc']; $this->status = $params['status']; } else { $this->id = 0; $this->title = ''; $this->desc = ''; $this->status = ''; } } public static function find($id) { if($id == null) { return null; } $sql = "select * from `news` where id=$id"; $result = mysql_query($sql); $rec = mysql_fetch_assoc($result); if($rec) { return new News($rec); } else { return null; } } public static function find_all() { $news = array(); $sql = "SELECT * FROM news ORDER BY id DESC"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $news[] = new News($row); } return $news; } public static function find_indexpage_news() { $news = array(); $sql = "SELECT * FROM news WHERE status=1 ORDER BY id"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $news[] = new News($row); } return $news; } public static function find_news($id=null,$start) { if($id) # search for specific category { $sql = "SELECT * from news WHERE id=$id"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); return new News($row); } else # search for all categories { $news = array(); $sql = "SELECT * FROM news ORDER BY id desc limit $start,9"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $news[] = new News($row); } return $news; } } public function id() { return $this->id; } public function title() { return $this->title; } public function desc() { return $this->desc; } public function status() { return $this->status; } public function save() { if($this->id == 0) # got new object run insert query { $sql = "INSERT INTO news (`title`,`desc`,`status`)VALUE('".$this->title."','".$this->desc."',0)"; mysql_query($sql); $this->id = mysql_insert_id(); } else { $sql = "UPDATE news SET `title`='".$this->title."', `desc`='".$this->desc."' WHERE `id`='".$this->id."'"; mysql_query($sql); } } public function edit($params,$id) { $sql = "UPDATE news SET `title`='".$params['title']."', `desc`='".$params['desc']."' WHERE `id`='".$id."'"; mysql_query($sql); } public function update($params) { $this->title = $params['title']; $this->desc = $params['desc']; } static public function delete($id) { mysql_query("DELETE FROM news WHERE `id`='".$id."'") or die(mysql_error()); } public static function Count() { $sql = "SELECT count(id) AS news from news"; $result = mysql_query($sql); echo mysql_error(); $row = mysql_fetch_assoc($result); return $row['news']; } } ?>