0

我已经变得相当卡住了我的情况。我有一个包含字段 id、showid、season、episode 和其他一些任意数据的数据库。我有一个 php 页面来显示有关节目剧集的信息,这些信息通过将 id 作为获取变量来工作。我希望实现的是链接到该电视节目的下一集和上一集,但是节目不一定按顺序排列,因此简单地增加或减少 id 将无济于事。所以我会获取剧集并$rows使用以下MySQL查询存储:

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

现在不必遍历数组(对于某些节目来说可能相当大),有没有一种简单的方法可以让我找到下一集和上一集?我敢肯定,我只是想不出来。

编辑: 我自己一直在考虑这个问题,但还没有任何有用的东西(尽管感谢您的帮助,非常感谢)。我想到的是进行查询以查看同一季节是否存在更高的剧集,如果不存在则进行另一个查询以找到下一个最低季的最低剧集 - 或类似的东西 - 然后重复上一个。你怎么看?

编辑2: 我想我会发布我的最终代码以供将来参考。在这里,我找到了 nex 和以前的节目(如果存在)的 id。

查找下一集:

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());
}

查找上一集:

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());
}

然后我有以下链接:

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

3 回答 3

1

为什么不像表格的分页器那样做,而是只显示一个条目你的实际页面。您可以通过像这样扩展您的查询来做到这一点:

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

其中 0 是偏移量。这意味着您必须增加的起始记录。这里作为一个例子:

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

然后,您只需在您的 URL 中将实际显示的剧集的页面作为 GET 参数“?page=0”传递。上一集是 page - 1,下一集是 page + 1。

唯一的缺陷是您必须检查拳头和最后一集。要知道上一个或下一个是不可能的。

于 2013-07-30T19:06:24.503 回答
0

我在这个线程中发布了 3 次我的错,但如果你的剧集和 ID 没有被订购,那么据我所知,没有办法做到这一点

修正你的身份证

于 2013-07-30T19:52:45.510 回答
0

如果 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));

这不会处理您已经优雅地在最后一集的情况,但应该让您开始。

于 2013-07-30T18:59:19.627 回答