简体   繁体   中英

SQL for sorting out next/previous picture in gallery

I'm working on the photo section of my website, but more precisely on the next/previous link with sorting options.

There are two different option for sorting that exist, first there is a filter (Latest, Trending, Favorite, User etc.) and secondly a sorting option for some of the previous filters (date, views, ratings etc.)

I am having trouble finding a good solution for easily retrieving the next/previous picture for my slideshow.

This is the little bit of SQL I have for getting the next picture for a filter:latest

if ($filter == "latest") {
        $sql = "SELECT   *
                FROM     photos
                WHERE    status = 0 AND id < ".$id."
                ORDER BY id DESC
                LIMIT    1
        ";
        $result = $db->sql_query($sql);
        $sql2 ="SELECT   *
                FROM     photos
                WHERE    id = (SELECT MAX(id) FROM photos WHERE status = '0')
                ORDER BY id DESC
                LIMIT    1
    ";
    }

I am wondering if there is an easier way to implement this in my project ? maybe a php class exists to do something like this ?

I feel like I'm going to spend a long time getting all these SQL queries to work properly if I have to do it all like this.

Edit

This is the layout of my photos table

`id`              -> unique ID, autoincremented
`title`         
`img_name`        -> contains the image file name 
`date_created`    -> when the picture was uploaded
`uploader`        -> id of the user that uploaded
`views`           -> number of views the picture has
`up_votes`        -> votes used to calculate the Wilson score interval
`down_votes`
`net_votes`       -> up vote minus down votes

There are other tables like one that links user with friends they have, it is called users_friends

`user_id`        -> contains id of the user that added the friend 
`friend_user_id` -> contains id of the friend in question

With these two tables I am able to get all the pictures posted by the friend of a user using this sql query :

SELECT * 
FROM photos 
WHERE uploader IN 
            (
                SELECT friend_user_id 
                FROM users_friends 
                WHERE user_id = ':user_id' 
             ) 
ORDER BY id DESC

I then have two queries to get the link to the next picture and 2 other queries for the previous one. I have two because one is for when you get at the end of the database to go back to the other end and the other one is for all the other queries. And they look something like this (I'm only posting the next button here)

$sql = "SELECT   *
                FROM     photos
                WHERE    status = 0 AND id < ".$id." AND Uploader IN (SELECT friend_user_id FROM users_friends WHERE user_id = ".$logedInUser['User_ID'].")
                ORDER BY id DESC
                LIMIT    1
        ";
$sql2 ="SELECT   *
                FROM     photos
                WHERE    id = (SELECT MAX(id) FROM photos WHERE status = '0' AND Uploader IN (SELECT friend_user_id FROM users_friends WHERE user_id = ".$logedInUser['User_ID']."))
                ORDER BY id DESC
                LIMIT    1
        ";
$result = $db->sql_query($sql);

if (mysql_num_rows($result)==1) { 
        $row = $db->sql_fetchrow($result);
        return "/photo/".$row["id"]."/".urlFriendly($row["Title"])."/".$filter.$sort;
} 
else 
{
        $result = $db->sql_query($sql2);
        $row = $db->sql_fetchrow($result);
        return "/photo/".$row["id"]."/".urlFriendly($row["Title"])."/".$filter.sort;
}

I feel like there is a lot of stuff that could be simplified, but I don't yet know how to do it.

I also have the Lower bound of Wilson score sql code to implement for the next/previous button, and it is quite a bit sql code to have to write 4 time. Maybe that I can insert the Wilson score in the photos table, which could simplify a lot the sql code.

It would be helpful if you could post the structure of your table - ie what columns/types you have and what each row represents. I'll assume that you have a table where each row represents a photo, and that each photo has an ID, filename, and various other columns that help you filter/sort (such as date, favorite, etc.).

If your filtering and sorting preferences are defined by the user, you can then use those in a single SQL query, whereby you get the full set of ordered results. For instance:

$sort = date;
$filter = favorite;

$sql = "SELECT ID, filename, [other vars here]
        FROM photos
        WHERE " . $filter . " = TRUE
        ORDER BY ". $sort . " DESC";
$result = $db->sql_query($sql);

Now, you can use a function such as mysql_fetch_array to step through each row in your result set and populate some kind of data structure with the current, previous, and next photos.

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