简体   繁体   中英

creating a photo album gallery

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)

在此处输入图片说明

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

在此处输入图片说明

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

在此处输入图片说明

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.

You need to add an album_id to the gallery table.

This field would be a foreign key linked to the albums table.

This will allow you to query the table without JOIN while giving you data integrity.

This assume you want one album per image.

Some ecommendations:

You need to decide

  • whether all images are stored in only one album.
  • Each image has at least one album assigned
  • images can be "orphan" - no album assigned currently
  • albums may be empty

Set up a RDS scheme like

  album ---(1:n)---> image (where n = 0 - infinite)

or

  album <---(n:n) ---> image

or

  image ----(n:0) ----> album (!!)

Next:

  • your join matches ... WHERE gallery.album = albums.album_id ...

gallery.album contains the album title (varchar) and album.album_id an id (auto index?). That won't work and isn't best practice: the link between album and gallery image must be by an album id, otherwise it will be hard to rename an album later on.

I haven't checked further as these are basics to be sorted out first.

I would use ID numbers to reference across tables. But this Album title should work ok. It is just if you allow users to change the name of the album later you have to update all gallery rows and not just the Album title for that album.id

Try

SELECT
albums.album_title,
gallery.id,
gallery.added,
gallery.album,
gallery.title,
gallery.description,
gallery.file_name
FROM
albums
LEFT JOIN gallery ON albums.album_title = gallery.title ORDER BY gallery.id DESC LIMIT (you want here)

This will go through all Albums and then output the corresponding images in gallery id order from high to low.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM