So I was creating a simple photo gallery site, wherein users can upload their photos or delete their photos. While, I have a CRUD functions as well in the side of the users, where I can add users, delete users, update user's info and search user's info.
Now, I want photos that will be organized by an album. And so I created a table in MySQL Database an 'album' table where there's album_id, album_name and album_desc propert in it. And, I also add a property to my photo class an album_id property. Also, I have an album class, which this is my problem.
What should be the content of my album class?
How will I organize those photos by an album?
<?php
require_once(LIB_PATH.DS.'database.php');
class Album extends DatabaseObject{
protected static $table_name="album";
protected static $db_fields=array('album_id', 'album_name', 'album_desc');
public $id;
public $name;
public $desc;
public static function find_all() {
return self::find_by_sql("SELECT * FROM ".self::$table_name);
}
public static function find_by_id($id=0) {
$result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE id={$id} LIMIT 1");
return !empty($result_array) ? array_shift($result_array) : false;
}
public static function find_by_sql($sql="") {
global $database;
$result_set = $database->query($sql);
$object_array = array();
while ($row = $database->fetch_array($result_set)) {
$object_array[] = self::instantiate($row);
}
return $object_array;
}
public static function count_all() {
global $database;
$sql = "SELECT COUNT(*) FROM ".self::$table_name;
$result_set = $database->query($sql);
$row = $database->fetch_array($result_set);
return array_shift($row);
}
private static function instantiate($record) {
$object = new self;
foreach($record as $attribute=>$value){
if($object->has_attribute($attribute)) {
$object->$attribute = $value;
}
}
return $object;
}
private function has_attribute($attribute) {
return array_key_exists($attribute, $this->attributes());
}
protected function attributes() {
$attributes = array();
foreach(self::$db_fields as $field) {
if(property_exists($this, $field)) {
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
protected function sanitized_attributes() {
global $database;
$clean_attributes = array();
foreach($this->attributes() as $key => $value){
$clean_attributes[$key] = $database->escape_value($value);
}
return $clean_attributes;
}
public function create_album() {
global $database;
$attributes = $this->sanitized_attributes();
$sql = "INSERT INTO ".self::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES ('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";
if($database->query($sql)) {
$this->id = $database->insert_id();
return true;
} else {
return false;
}
}
public function delete_album() {
global $database;
$sql = "DELETE FROM ".self::$table_name;
$sql .= " WHERE id=". $database->escape_value($this->id);
$sql .= " LIMIT 1";
$database->query($sql);
return ($database->affected_rows() == 1) ? true : false;
}
}
//$album = new Album();
?>
Also, I have a album_view_this.php wherein. I have this snippet code and got an error with it.
<?php
$albums = Album::find_all();
?>
<table class="bordered" cellpadding="10">
<tr>
<th>ID</th>
<th>USER</th>
<th>NAME</th>
<th> </th>
<th> </th>
</tr>
<tr>
</tr>
<?php
$count=0;
foreach($albums as $album):
$count++;
?>
<tr>
<td><?php echo $albums->album_id;?></td>
</tr>
</table
that was just a single line that makes my mind crazy because of the error:
THE ERROR -> Notice: Trying to get property of non-object in