Reputation: 241
Im trying to make a album with next/previous image. Could I do something like this or isnt this a good idea? I dont really know how it could be done any other way.
<?php
$imgnl = $_POST['next'];
$imgnl = $_POST['last'];
if($imgnl == "next"){
$result=mysqli_query($con, "SELECT * FROM album WHERE user_id='$id AND (added>'$currentimg') LIMIT 1");
$row=mysqli_fetch_array($result);
$currentimg = row['added']; // Y-m-d H:i:s
$url = row['url'];
}else if($imgnl == "last"){
$result=mysqli_query($con, "SELECT * FROM album WHERE user_id='$id AND (added<'$currentimg') LIMIT 1");
$row=mysqli_fetch_array($result);
$currentimg = row['added']; // Y-m-d H:i:s
$url = row['url'];
}
else{
$result=mysqli_query($con, "SELECT * FROM album WHERE user_id='$id ORDER BY added ASC LIMIT 1");
$row=mysqli_fetch_array($result);
$currentimg = row['added']; // Y-m-d H:i:s
$url = row['url'];
}
?>
<img src $url ......
Upvotes: 0
Views: 45
Reputation: 971
Pulling the next N records from the database based on the sorted record keys being greater or less than the value of the current pointer...works. Just make sure the key values are unique and are sorted (indexed) the way you want. You could get fancier in your code like combining the 2 SQL statements into 1...but there's no need. Your approach works. (I also noticed that your SQL statements are vulnerable to injection, but that's icing on the cake, too.) It looks like you're on the right track and your code should run soon, if not already.
Upvotes: 1