I've created a photo gallery where the user can upload images, but I'm trying to make it where the users can create photo albums to store their images in.

The process works like this. 

1. The User starts by creating an album (the album is stored in the album table like this)

![enter image description here](https://i.stack.imgur.com/yLnqX.png)

2. Then the user uploads images into the album. (the images are stored
in the gallery table like this)

![enter image description here](https://i.stack.imgur.com/vvuuY.png)

What I'm trying to accomplish is getting the images to load based on the album they're in. Like this.

![enter image description here](https://i.stack.imgur.com/KylYi.png)

So on "Test Album 1" I only want the photos in that album to load and the same with "Test Album 2".

I created an **album.php** page that I want the images to load on based on what album they are in.
For the two links that are shown in the image, I call them like this. Based off of the albums table.

`<a href="../gallery/album.php?=<?php echo escape ($ga->album_id);?>">`

Now here is where I'm lacking experience / knowledge. I need to join the two tables together and I can't figure out how to write this query correctly.

I want to query the albums table, album\_id and Join it with the gallery table.

Here is the query I have as of now and need help with.

```
  $gallery = DB::getInstance()->query("SELECT
albums.album_id,
gallery.id,
gallery.added,
gallery.album,
gallery.title,
gallery.description,
gallery.file_name
FROM
albums
LEFT JOIN gallery ON albums.album_id = gallery.id WHERE gallery.album = albums.album_id ORDER BY gallery.id DESC LIMIT $start, $limit");

```

Then I call my images like this.

```
foreach($gallery->results() as $g){
    ?>
    <li>
    <a class="fancybox" rel="<?php echo escape($g->album); ?>"
    href="../images/gallery/<?php echo escape($g->file_name); ?>">
    <img src="../images/gallery/<?php echo escape($g->file_name); ?>" class="max-img-border"></a>
    <div class="galleryh"><?php echo escape($g->title); ?></div>
    <div class="galleryp"><?php echo escape($g->description); ?></div>
</li>

    <?php
}
?>

```

If I can just figure out how to write that JOIN query correctly I will be good to go.
Any solutions would be greatly appreciated.