cgm225 Posted March 13, 2008 Share Posted March 13, 2008 Below is a procedural script I use at the core of my image/video gallery to gather all the information I need to display images/video. I want to make this script object oriented, but do not know where to start? Could someone help me? Also, any other feedback would be greatly appreciated! Thank you all in advance! <?php //Enable full error reporting //error_reporting(E_ALL); /*//////General gallery includes, variables, etc.//////////////////////////////////////////////////////////// */ include_once "gallery.configurations.inc.php"; include_once "gallery.functions.inc.php"; //Getting required variables $id = $_GET['id']; $album = $_GET['album']; $page = $_GET['page']; if (!$page) {$page = 1;} $image = $_GET['image']; /*//////////////////////////////////////////////////////////////////////////////////////// // // Database Name: Gallery // Database Structure // // Albums Table: id || full_title || short_title || dir || timestamp // | // +----------------------------------------------------+ // | // Images Table: id || filename || caption date || location || album || timestamp // | // +------------------+ // | // Videos Table: id || filename || title || album || timestamp // ////////////////////////////////////////////////////////////////////////////////////////*/ //Connecting to MySQL gallery database $mysql_server_connection_1 = mysql_connect($mysql_server_1,$mysql_server_1_username,$mysql_server_1_password,new_link); mysql_select_db($gallery_database, $mysql_server_connection_1); //Will create tables (as outlined above), if not already present include_once "action_table_generation.inc.php"; //Gathering information and running actions for the pages with multiple photo and videos if ($album != ""){ //If there is no album directory by that name, will send the user to an error page if (($album != "") AND (!file_exists("$server_photo_album_location/$album/")) AND (!file_exists("$server_video_album_location/$album/"))) { header("Location:http://www.example.com/home/error");} //If the photo album is restricted by .htaccess, will also add authentication as well if ((file_exists("$server_photo_album_location/$album/.htaccess")) OR (file_exists("$server_video_album_location/$album/.htaccess"))) { general_permissions();} /* Will determine the number of photos to display based on a default value or on a user selected value from a form at the bottom of the page main album page */ if (!$_POST["number_displayed"]) { if (!$_SESSION['number_displayed']) { $number_displayed = 2; } else { $number_displayed = $_SESSION['number_displayed']; } } else { $number_displayed = $_POST["number_displayed"]; $_SESSION['number_displayed'] = $number_displayed; } //Bringing in album information from MySQL gallery database, album table $album_query = "SELECT * FROM albums WHERE dir = '$album'"; $album_results = mysql_fetch_array(mysql_query($album_query,$mysql_server_connection_1)); $album_id = $album_results['id']; $full_title = $album_results['full_title']; /*//////Adding any new images to the MySQL gallery database//////////////////////////////////////////// */ $number_of_image_files = count(glob("$server_photo_album_location/$album/*.*")); $number_of_mysql_image_entries = mysql_num_rows(mysql_query("SELECT * FROM images WHERE album = '$album_id'",$mysql_server_connection_1)); if ($number_of_image_files != $number_of_mysql_image_entries) { include_once "action_add_images.inc.php";} /*//////Retrieving the total number of photos and photo pages////////////////////////////////////////// */ //Finding the total number of images for any particular album from MySQL gallery database, images table $image_query = "SELECT * FROM images WHERE album = $album_id"; $number_of_images = mysql_num_rows(mysql_query($image_query,$mysql_server_connection_1)); //Calculating total number of photo pages $number_of_photo_pages = ceil($number_of_images / $number_displayed); /*//////Generating image thumbnails if not already done//////////////////////////////////////////////// */ $number_of_thumbnail_files = count(glob("$server_photo_thumbnail_location/$album/*.*")); if ($number_of_thumbnail_files != ($number_of_images * 2)) { include_once "action_thumbnail_generation.inc.php";} /*//////Adding any new videos to the MySQL gallery database//////////////////////////////////////////// */ $number_of_video_files = count(glob("$server_video_album_location/$album/*.*")); $number_of_mysql_video_entries = mysql_num_rows(mysql_query("SELECT * FROM videos WHERE album = '$album_id'",$mysql_server_connection_1)); if ($number_of_video_files != $number_of_mysql_video_entries) { include_once "action_add_videos.inc.php";} /*//////Retrieving the total number of videos and video pages////////////////////////////////////////// */ //Finding the total number of videos for any particular album from MySQL gallery database, videos table $query = "SELECT * FROM videos WHERE album = $album_id"; $number_of_videos = mysql_num_rows(mysql_query($query,$mysql_server_connection_1)); //Calculating total number of video pages $number_of_video_pages = ceil($number_of_videos / $number_displayed); } //Gathering information and running actions for the pages with a single photo if ($image != "") { //Retrieving query of image for any one particular page $query = "SELECT * FROM images WHERE album = $album_id ORDER BY filename ASC LIMIT $image, 1"; $specific_image = mysql_query($query,$mysql_server_connection_1); while($row = mysql_fetch_array($specific_image)) { $image_id = $row['id']; $filename = $row['filename']; $caption = stripslashes($row['caption']); $date = stripslashes($row['date']); $location = stripslashes($row['location']);} } ?> Quote Link to comment Share on other sites More sharing options...
keeB Posted March 13, 2008 Share Posted March 13, 2008 Break everything in to a 'piece' of the puzzle. <?php class AlbumDao { private $dbconnection; public function getImageByName($dir) { //find an image in a certain directory; } public function getImageById($id) { //select * from images where id = id } public function initialize($albumName) { // select * from album a // inner join images i on i.album = a.id // inner join videos v on v.album = a.id // where album.short_tile = '$albumName' } public function setDbConnection(DatabaseConnection $dbconnection) {$this->dbconnection = $dbconnection;} } class Album { private $name; private $title; private $dir; private $dao; public function __construct($name) { $this->name = $name; $this->init(); } private init() { $this->dao->initialize($this->name); } public function getImageList() { // some helper functions which return a list of Image objects } public function getVideoList() { // some other helper functions } public function setDao(AlbumDao $dao) {$this->dao = $dao} } class Image { private $filename; private $caption; public function __construct() { } public function draw() { // echo <img src .... } } ?> I just wrote this object diagram as I went along, there could be some oversights, but this is how I would start. Quote Link to comment Share on other sites More sharing options...
cgm225 Posted March 13, 2008 Author Share Posted March 13, 2008 Thank you so much for your help! What does DAO stand for? Quote Link to comment Share on other sites More sharing options...
cgm225 Posted March 13, 2008 Author Share Posted March 13, 2008 Also, could you give me an example of how you would use the code you posted after all the classes had been included in a script? Quote Link to comment Share on other sites More sharing options...
keeB Posted March 14, 2008 Share Posted March 14, 2008 DAO = Data Access Object http://en.wikipedia.org/wiki/Data_Access_Object Here's a snippet of how I would display some family album. <?php $configuration = Configurator::getInstance(); // reads the configuration file which contains database info, paths, etc $db = new DatabaseFactory::getInstance($configuration->database->getType()); $album = new Album("Family Photos"); // who knows where you got the name from. Maybe a script which printed all listed albums. $a->setDao($db); // make sure it's using our database echo "Photo's in this album"; foreach($a->getImageList() as $images) { $image->draw(); } echo "Videos in this album"; foreach($a->getVideoList() as $video) { $video->draw(); } ?> Quote Link to comment Share on other sites More sharing options...
cgm225 Posted March 14, 2008 Author Share Posted March 14, 2008 Thank you.. and I am sorry about all the follow-up questions.. but could you talk a little more about the initialize function and what it does/ how to code that? Thank you again! Quote Link to comment Share on other sites More sharing options...
keeB Posted March 14, 2008 Share Posted March 14, 2008 Sure. It initializes the AlbumList object with references to constructed Images and Videos. Think of what an album is. It's a collection of things with a given name. So, that's the way I have modeled it. Depending on how abstract you want to make your app, AlbumDao.initialize() could return raw information (a ResultSet or something along those lines) from the database, or, it could [the more OOP way] return Image and Video objects which the AlbumList stores however it sees fit. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.