Jump to content

Creating Album in Your Photo Gallery


booxvid

Recommended Posts

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() { 
	// return an array of attribute names and their values
  $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;

  $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;

}


}

?>

 

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

Link to comment
Share on other sites

Your relation is: Album has many photos. Right?

 

Your album class should have an structure to store your photos, maybe an array or something like that.

 

class Album {
    private $id;
    private $name;
    private $description;
    private $photos = array();
}

Link to comment
Share on other sites

As a slight digression, never, ever, ever use 'global'.  For anything.  In any context, but especially in an OOP context.  Ever.  The 'global' keyword creates incredibly brittle code, tying/coupling what should be modular and reusable code to the environment in which it is invoked.

 

What you should be doing is storing an instance of your $database as a protected member of your abstract base class.  It should be passed in via its constructor.  This is known as Dependency Injection*, and is the way to go.

 

Even in a procedural context, 'global' is bad.  It creates an implicit requirement for a function to work properly by completely circumventing its signature.  Anything a function needs from an exterior scope in order for it to work should be passed into it via its argument list.  That's why it's there

 

There is nothing to be gained by using 'global' except for inevitable headaches down the road.  Any resource using 'global' should be considered suspect.  And, yes, that includes apps like WordPress and Joomla, and whatever frameworks that are out there that do it.

 

*Usually DI is a bit more complicated than that, but the basic idea is that if a class depends on an exterior object in order to do its job, it should contain a reference to that exterior object.  The most common way to grab a hold of that reference is to pass it into a constructor.  That said, it's certainly acceptable to simply pass it into whatever method(s) that need to use it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.