我已经变得相当卡住了我的情况。我有一个包含字段 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>";