简体   繁体   English

从MySQL数据库中查找下一个/上一个电视节目

[英]finding next/previous tv show from MySQL database

I've become rather stuck with a situation I have. 我已经陷入一种困境。 I have a database with fields id, showid, season, episode and some other arbitrary data. 我有一个数据库,其中包含字段ID,ShowID,季节,情节和其他一些任意数据。 I have a php page to display information about episodes of shows which works by having the id as a get variable. 我有一个php页面,用于显示有关剧集的信息,通过将id作为get变量可以起作用。 What I wish to achieve is to have a link to the next episode and previous episode of this tv program however the shows aren't necessarily in order and so simply incrementing or decrementing the id will not help. 我希望实现的目标是链接到该电视节目的下一集和上一集,但是节目不一定按顺序进行,因此简单地增加或减少ID将无济于事。 So I would fetch the episodes and store then in $rows with the following MySQL query: 因此,我将获取这些情节,然后使用以下MySQL查询将其存储在$rows

SELECT id FROM episodes WHERE showid = $id ORDER BY season, episode;

Now without having to loop through the array (which could be rather large for some shows) is there an easy way for me to find the next and previous episode? 现在不必遍历整个数组(对于某些节目来说可能很大),是否有一种简单的方法可以让我找到下一集和上一集? I'm sure there is I just can't think of it. 我敢肯定,我只是想不到。

Edit: I have been thinking about this myself and nothing all that helpful has come through yet (although thanks for the help, its much appreciated). 编辑:我自己一直在考虑这个问题,并且还没有实现所有有用的功能(尽管感谢您的帮助,非常感谢)。 What I have thought of is to make a query to see if a higher episode exists of the same season, if not make another query to find the lowest episode of then next lowest season - or something along those lines - and then repeat for previous. 我想到的是进行查询,以查看同一季节是否存在较高的情节,如果不进行其他查询,则查找下一个最低季节的最低情节(或类似情况),然后重复上一个。 What do you think? 你怎么看?

Edit 2: I thought I would post my final code for future reference. 编辑2:我以为我会发布最终代码以供将来参考。 Here I find the id's of the nex and previous shows (if they exist). 在这里,我找到了nex和以前的节目(如果存在)的ID。

Find the next episode: 查找下一集:

try {
    // First check for a show of the same season but the next lowest episode number
    $stmt = $dbh->prepare("SELECT id FROM episodes WHERE showid = :show AND season = :s AND episode > :e ORDER BY episode LIMIT 1;");
    $stmt->execute($pars);
    $rows = $stmt->fetchAll();
    if(count($rows) > 0) { // If there is one this is our next id
        $nextid = $rows[0]['id'];
    } else {
        // Otherwise check for an episode of a higher season number
        $stmt = $dbh->prepare("SELECT id FROM episodes WHERE showid = :show AND season > :s ORDER BY season, episode LIMIT 1;");
        $stmt->execute($pars);
        $rows = $stmt->fetchAll();
        if(count($rows) > 0) { // If there is one this is our next id.
            $nextid = $rows[0]['id'];
        } // Otherwise no next id is set and so no next link will be created.
    }
} catch(PDOException $e) {
    die("Error: " . $e->getMessage());
}

Find the previous episode: 查找上一集:

try {
    // First check for an episode in same season of the next highest episode number.
    $stmt = $dbh->prepare("SELECT id FROM episodes WHERE showid = :show AND season = :s AND episode < :e ORDER BY episode DESC LIMIT 1;");
    $stmt->execute($pars);
    $rows = $stmt->fetchAll();
    if(count($rows) > 0) { // If one is found then this is our prev id.
        $previd = $rows[0]['id'];
    } else {
        // Otherwise check for an episode of a lower season number.
        $stmt = $dbh->prepare("SELECT id FROM episodes WHERE showid = :show AND season < :s ORDER BY season DESC, episode DESC LIMIT 1;");
        $stmt->execute($pars);
        $rows = $stmt->fetchAll();
        if(count($rows) > 0) { // If there is one this is our prev id.
            $previd = $rows[0]['id'];
        } // Otherwise no prev id is set and so no prev link will be created.
    }
} catch(PDOException $e) {
    die("Error: " . $e->getMessage());
}

Then I have the following links: 然后,我有以下链接:

if($previd) echo "<a href=\"/tv/view/" . $previd . "/\">Previous Episode</a>";
if($nextid) echo "<a href=\"/tv/view/" . $nextid . "/\">Next Episode</a>";

Why not doing it like a paginator of a table, but showing only one entry your actual page. 为什么不像桌子的分页器那样做,却只显示实际页面的一个条目。 You can do this by expading your query like this: 您可以这样扩展查询:

SELECT id FROM episodes WHERE showid = $id ORDER BY season, episode LIMIT 0,1;

Where the 0 is the offset. 其中0是偏移量。 That means the starting record, which you have to increment. 那意味着起始记录,您必须增加。 Here as an example: 这里举一个例子:

SELECT id FROM episodes WHERE showid = $id ORDER BY season, episode LIMIT $page,1;

Then you only have to pass the page of the actual shown episode as GET parameter "?page=0" in you URL. 然后,您只需将实际显示的情节的页面作为GET参数“?page = 0”传递到您的URL中。 Previous episode is page - 1 and next is page + 1. 上一集是第1页,下一页是第1页。

Only pitfall is that you have to check for fist and las episode. 唯一的陷阱是您必须检查拳头和小插曲。 To know when previous or next is not possible. 无法知道上一个或下一个何时。

If the ids aren't in any particular order, then you'll have to basically scan the table for where you currently are, and then use the last fetched and about-to-fetch records as your prev/next episodes, eg 如果ID的顺序不特定,则必须从根本上扫描表以了解当前位置,然后将上一次获取和即将获取的记录用作上一个/下一个情节,例如

$row = mysql_fetch_assoc($result);
$previous_episode = $row['id']; // prime the system
do {
   if ($row['id'] == $id_of_current_episode) {
       $next_row = mysql_fetch_assoc($result);
       $next_episode = $row['id'];
       break;
   }
} while($row = mysql_fetch_assoc($result));

This won't handle the case where you're already at the last episode gracefully, but should get you started. 如果您已经优雅地进入了最后一集,这将无法解决,但应该可以帮助您开始。

I posted 3 times in this thread my bad, but if your episodes and IDs aren't ordered then there's no way to do this as far as I know 我在此线程中发布了3次错误消息,但是,如果您未订购您的剧集和ID,那么据我所知没有办法

Fix your IDs 修正您的ID

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

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