简体   繁体   中英

Mysql image gallery with next and previous

I am making a photo gallery where you can view full size image and i want it to have next and previous buttons to cycle through images in full size. So far i managed to make it work but i have a problem when i delete and re-upload an image.

The problem is when i open the gallery where the image was deleted and re-uploaded and when i click on the image to view it in full size my next button disappears and there is only a previous button which takes you to the last image in the gallery and then the next button appears and it takes you to the first image when you click on it.

Here are my tables :

Gallery: id(int) primary key auto_increment, name varchar(255)
Photos : id(int) primary key auto_increment, path varchar(255), gallery_id int

Index.php:

<?php
$query=mysqli_query($conn,"SELECT*FROM gallery");
while($row=mysqli_fetch_array($query)){
$gallery_name=$row['name'];
$galleryid=$row['id'];
echo "<a href='gallery.php?id=$galleryid'>$gallery_name</a><br>";

}
?>

Gallery.php:

session_start();

<div class="piccontainer">
<?php
if(isset($_GET['id']) and is_numeric($_GET['id'])){
$id=$_GET['id'];
}else{
echo "<script>window.location.href='index.php'</script>";
}
$query=mysqli_query($conn,"SELECT*FROM gallery WHERE id='$id'");
while($row=mysqli_fetch_array($query)){
$gallery_name=$row['name'];
echo "<h2>".$gallery_name."</h2>";
}
$query=mysqli_query($conn,"SELECT*FROM photos WHERE gallery_id='$id'");
echo "<div><a class='nazad' href='index.php'>Back</a></div><br><br>";
while($row=mysqli_fetch_array($query)){
$pic_id=$row['id'];
$path=$row['path'];
$_SESSION['galleryid']=$row['gallery_id'];
echo "<div class='picholder'><a href='photo.php?id=$pic_id'><img src='uploads/$path'   /></a></div>";
}

?>
</div>

Photo.php:

session_start();

<div class="piccontent">
<?php
if(isset($_SESSION['galleryid'])){
$back=$_SESSION['galleryid'];
echo "<div><a class='nazad' href='gallery.php?id=$back'>Back</a></div><br><br>
";
}
?>
<?php
if(isset($_GET['id']) and is_numeric($_GET['id'])){
$id=$_GET['id'];

}else{
echo "<script>window.location.href='index.php'</script>";
} 
?>
<div id="links">
<?php
$prevquery=mysqli_query($conn,"SELECT id FROM photos WHERE id<$id AND gallery_id='$gid'");
while($row=mysqli_fetch_array($prevquery)){
$prev=$row['id'];
}
$nextquery=mysqli_query($conn,"SELECT id FROM photos WHERE id>$id AND gallery_id='$gid'");
while($rw=mysqli_fetch_array($nextquery)){
$next=$rw['id'];
}
$nextbtn=(isset($next))?"<a href='photo.php?id=$next'>Next</a>":"";
$prevbtn=(isset($prev))?"<a href='photo.php?id=$prev'>Prev</a>":"";
echo $prevbtn." ".$nextbtn;
?>
</div>
<?php
$query=mysqli_query($conn,"SELECT*FROM photos WHERE id='$id'");
while($row=mysqli_fetch_array($query)){
$path=$row['path'];


echo "<img src='uploads/$path' />";
}
?>
</div>

Help would be appreciated

I guess You need an ORDER BY and LIMIT 1 to get one row:

$prevquery=mysqli_query($conn,"SELECT id FROM photos WHERE id<$id AND gallery_id='$gid' ORDER BY id DESC LIMIT 1");

Do similar with the second query with ASC ORDER. This syntax is terrible anyway and open for SQL injection. Use prepared statements! I also suggest trying nested subquery in which You can inlude a query for previous and next photo ID.

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