简体   繁体   English

SQL整理画廊中的下一张/上一张图片

[英]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 这是我要为filter:latest获取下一张图片所用的SQL的一点点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 ? 也许存在一个php类来做这样的事情?

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. 我觉得我将花很长时间使所有这些SQL查询正常运行,如果我必须这样做的话。

Edit 编辑

This is the layout of my photos table 这是我的photos表的布局

`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 还有其他一些表,例如将用户链接到他们所拥有的朋友的表,称为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 : 使用这两个表,我可以使用此sql查询获取用户朋友发布的所有图片:

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. 我还为下一个/上一个按钮实现了Wilson得分下限的sql代码,并且必须要编写4次才能实现。 Maybe that I can insert the Wilson score in the photos table, which could simplify a lot the sql code. 也许我可以将Wilson得分插入到photos表中,这可以简化很多sql代码。

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.). 我假设您有一个表格,其中每行代表一张照片,并且每张照片都有一个ID,文件名以及其他各种可以帮助您进行过滤/排序的列(例如日期,收藏夹等)。

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. 如果过滤和排序首选项是由用户定义的,则可以在单个SQL查询中使用它们,从而获得全套有序结果。 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. 现在,您可以使用mysql_fetch_array之类的功能来遍历结果集中的每一行,并使用当前,上一张和下一张照片填充某种数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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