I have a photo gallery where I allow the user to create individual photo albums. I can display the name of each album like this.
$album = DB::getInstance()->query("SELECT `album_id`,`album_date`,`album_title` FROM `albums` ORDER BY `album_date` DESC");
What I'm trying to do is add a cover photo for each album, but can't seem to get my query correct. This is what I'm trying.
$album = DB::getInstance()->query("SELECT
albums.album_id,
albums.created,
albums.album_date,
albums.album_title,
gallery.file_name,
gallery.id,
gallery.album_id
FROM
albums
INNER JOIN gallery ON gallery.album_id = albums.album_id ORDER BY gallery.id DESC ;");
This is the result I get:
This is the result I'm trying to achieve:
I want each album name displayed with the last photo from each album as the cover photo.
If I add a LIMIT of 1 to my query, only one album is shown.
Here are my tables that are Joined together by a Foreign Key.
Albums Table:
Gallery Table:
Any solution to get this working would be much appreciated.