Photo Gallery With Album Using PHP/MYSQL

0
301

This tutorial is about creating Photo Gallery With Album Using PHP/MYSQL. Some gallery tutorials commonly for uploading and displaying only the photo. In this tutorial, we add some twist in our gallery in a very simple way. You can now create your own gallery with add album feature.

In this tutorial, we can count all the photos uploaded to that album, we can view the album also as well as the uploaded photo on it.

So first, create a database. Name it as any name you desire. In my case, I choose “itsourcecode” as the name of my database.

For the album table. Create a “gallery_albums” table then put the following attributes.

  1. <pre>CREATE TABLE `gallery_albums` (
  2.   `album_id` int(11) NOT NULL,
  3.   `album_name` text NOT NULL
  4. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;</pre>

Now, for the photos. Create a “gallery_photos” table then put the following attributes.

  1. <pre>CREATE TABLE `gallery_photos` (
  2.   `photo_id` int(11) NOT NULL,
  3.   `album_id` int(11) NOT NULL,
  4.   `photo_link` text NOT NULL
  5. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;</pre>

Now let’s start our PHP files.

First, create a “connection.php” file then put the following codes.

  1. &lt;?php
  2. $mysqli = new mysqli('localhost', 'root', '', 'itsourcecode');
  3. ?&gt;

On the “index.php” file, put the following codes.

  1. &lt;?php include 'connection.php'; ?&gt;
  2. &lt;!DOCTYPE html&gt;
  3. &lt;html&gt;
  4. &lt;head&gt;
  5. &lt;meta charset="UTF-8"&gt;
  6. &lt;title&gt;Gallery&lt;/title&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;h3&gt;Photo Gallery With Album in PHP/MYSQL&lt;/h3&gt;
  10. &lt;form method="post" action="add-album.php"&gt;
  11. &lt;label&gt;Add New Album:&lt;/label&gt;&lt;br&gt;
  12. &lt;input type="text" name="album_name" /&gt; &lt;input type="submit" name="submit_album" value="Add" /&gt;
  13. &lt;/form&gt;&lt;br&gt;
  14. &lt;?php
  15. if (isset($_GET['add_album_action'])) {
  16. if ($_GET['add_album_action'] == "successfull") { ?&gt;
  17. &lt;br&gt;New album created!&lt;br&gt;&lt;br&gt;
  18. &lt;?php }
  19. }
  20. ?&gt;
  21. &lt;?php
  22. $albums = $mysqli-&gt;query("SELECT * FROM gallery_albums");
  23. while ($album_data = $albums-&gt;fetch_assoc()) {
  24. $photos = $mysqli-&gt;query("SELECT * FROM gallery_photos WHERE album_id = ".$album_data['album_id']."");?&gt;
  25. &lt;b&gt;#&lt;?php echo $album_data['album_id'] ?&gt;&lt;/b&gt; &lt;a href="view-album.php?album_id=&lt;?php echo $album_data['album_id'] ?&gt;"&gt;&lt;?php echo $album_data['album_name'] ?&gt;&lt;/a&gt; (&lt;?php echo $photos-&gt;num_rows; ?&gt;)&lt;br&gt;&lt;br&gt;
  26. &lt;?php }
  27. ?&gt;
  28. &lt;/body&gt;
  29. &lt;/html&gt;

For creating an album, create a “add-album.php” file then put the following codes.

  1. &lt;?php
  2. include 'connection.php';
  3. if (isset($_POST['submit_album'])) {
  4. $album = $_POST['album_name'];
  5. $add_album = $mysqli-&gt;query("INSERT INTO gallery_albums (album_name) VALUES ('$album')");
  6. if ($add_album) {
  7. header("Location: index.php?add_album_action=successfull");
  8. } else {
  9. echo $mysqli-error;
  10. }
  11. }
  12. ?&gt;

For viewing the upload photo on the album, create a “view-album.php” file then put the following codes.

  1. &lt;?php
  2. include 'connection.php';
  3. if (isset($_GET['album_id'])) {
  4. $album_id = $_GET['album_id'];
  5. $get_album = $mysqli-&gt;query("SELECT * FROM gallery_albums WHERE album_id = $album_id");
  6. $album_data = $get_album-&gt;fetch_assoc();
  7. } else {
  8. header("Location: index.php");
  9. }
  10. ?&gt;
  11. &lt;!DOCTYPE html&gt;
  12. &lt;html&gt;
  13. &lt;head&gt;
  14. &lt;meta charset="UTF-8"&gt;
  15. &lt;title&gt;&lt;?php echo $album_data['album_name'] ?&gt;&lt;/title&gt;
  16. &lt;/head&gt;
  17. &lt;body&gt;
  18. &lt;?php
  19. $photo_count = $mysqli-&gt;query("SELECT * FROM gallery_photos WHERE album_id = $album_id");
  20. ?&gt;
  21. &lt;a href="index.php"&gt;Home&lt;/a&gt; | &lt;?php echo $album_data['album_name'] ?&gt; (&lt;?php echo $photo_count-&gt;num_rows; ?&gt;)&lt;br&gt;&lt;br&gt;
  22. &lt;form method="post" action="upload_photo.php?album_id=&lt;?php echo $album_id ?&gt;" enctype="multipart/form-data"&gt;
  23. &lt;label&gt;Add photo to this album:&lt;/label&gt;&lt;br&gt;
  24. &lt;input type="file" name="photo" /&gt; &lt;input type="submit" name="upload_photo" value="Upload" /&gt;
  25. &lt;/form&gt;
  26. &lt;?php
  27. if (isset($_GET['upload_action'])) {
  28. if ($_GET['upload_action'] == "success") { ?&gt;
  29. &lt;br&gt;&lt;br&gt;Photo successfully added to this album!&lt;br&gt;&lt;br&gt;
  30. &lt;?php }
  31. }
  32. ?&gt;
  33. &lt;?php
  34. $photos = $mysqli-&gt;query("SELECT * FROM gallery_photos WHERE album_id = $album_id");
  35. while($photo_data = $photos-&gt;fetch_assoc()) { ?&gt;
  36. &lt;img src="&lt;?php echo $photo_data['photo_link'] ?&gt;" width="200px" height="200px" /&gt;
  37. &lt;?php }
  38. ?&gt;
  39. &lt;/body&gt;
  40. &lt;/html&gt;

And last, for adding or uploading a photo to the album. Create a “upload-photo.php” file then put the following codes.

  1. &lt;?php
  2. include 'connection.php';
  3. $album_id = $_GET['album_id'];
  4. if ($_FILES['photo']['name'] != null) {
  5. move_uploaded_file($_FILES['photo']['tmp_name'], "images/". $_FILES['photo']['name']);
  6. $photo_link = "images/". $_FILES['photo']['name'];
  7.  
  8. $upload_photo = $mysqli-&gt;query("INSERT INTO gallery_photos (album_id, photo_link) VALUES ($album_id, '$photo_link')");
  9. if ($upload_photo) {
  10. header("Location: view-album.php?album_id=$album_id&amp;upload_action=success");
  11. } else {
  12. echo $mysqli-&gt;error;
  13. }
  14. } else {
  15. header("Location: index.php");
  16. }
  17. ?&gt;

Photo Gallery With Album Using PHP/MYSQL Screenshots:

IF you have questions about Photo Gallery With Album Using PHP/MYSQL feel free to ask us by commenting below or by contacting us by visiting on our contact page. THANK YOU.

Facebook Comments
(Visited 312 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here