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>&nbsp;</th>
       <th>&nbsp;</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

Recommended Answers

All 2 Replies

i don't understand your question do you want image in album table belong to perticular album then you have to also insert the id of image from your image or photo table in album table

Thats because $albums = Album::find_all(); returns an array

public static function find_all() {
    return self::find_by_sql("SELECT * FROM ".self::$table_name);
}
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;
}

You will want a starter line like $album = new Album(); and careful not to overwrite the object later on!

The structure you want is:

album table
album_id,album_title....etc - no data about any photo

and either

photo table
photo_id,album_id,..... - only photo data(and if no reference table, include the album id)

or

photo_album_ref table
id,album_id,photo_id - links up that photos 3, 5 and 11 are in album 1 etc

the latter reference table has the benefit of being able to assign a single photo to any number of albums

Then you just find out what photos are in a album by either:

SELECT * FROM albums a 
LEFT JOIN photos p ON a.album_id = p.album_id
WHERE a.album_id = 3

SELECT * FROM albums a
LEFT JOIN photo_album_ref `par` ON a.album_id = par.album_id
LEFT JOIN photos p ON par.photo_id = p.photo_id
WHERE a.album_id = 3;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.