搜索结果页面的上一页或下一页10链接

时间:2015-03-03 23:06:51

标签: php mysql pdo

我正在尝试创建一个PDO数据库结果页面,其中包含前一个和下一个10个或下一个10个记录的前一个和下一个。我只是想不通应该在4 ?????下面的区域。我尝试了几种不同的方法,各种不正确的结果。关于缺失部分的任何想法,使这项工作?

$results = $dbh->prepare("select 
stories.SID,
stories.story_name,
stories.category,
stories.genre
FROM stories
WHERE stories.category = :cat OR stories.genre = :gen LIMIT 10");
$results->bindParam(':cat', $scategory, PDO::PARAM_STR);
$results->bindParam(':gen', $sgenre, PDO::PARAM_STR);
$results->execute();
$row = $results->fetchAll(PDO::FETCH_ASSOC);
}


if ($row) {
echo '<table>';
echo '<tr>';
foreach ($row as $all) {
echo '<tr>';
echo "<td><a href=\"http://example.com/?w=$all[SID]\">$all[story_name]</a>
</td>";
echo "<td>$all[category]</td>";
echo "<td>$all[genre]</td>";
}
echo '</table>';
}


//get all results from PDO SQL query and pass to previous & next links
$id = $row;

$stmt_a = $pdo->prepare("
(SELECT * FROM stories WHERE ???? ORDER BY id DESC LIMIT 1)
UNION (SELECT * FROM stories WHERE id = (SELECT MAX(id) FROM stories)) LIMIT
1 prev_id");
$stmt_b = $pdo->prepare("
(SELECT * FROM stories WHERE ????? ORDER BY id ASC LIMIT 1)
 UNION (SELECT * FROM stories WHERE id = (SELECT MIN(id) FROM stories)) LIMIT
 1 next_id");

// $vars = array(':id' => $id);
$prev = $stmt_a->execute();
$next = $stmt_b->execute();

if ($prev) {
while($row = $stmt_a->fetchObject()) {
echo '<a href="' ??????? '">Previous</a>';
} 
} else {
echo 'no previous';
}

if ($next) {
while($row = $stmt_b->fetchObject()) {
echo '<a href="' ??????? '">Next</a>';
}
} else {
echo 'no next';
}

1 个答案:

答案 0 :(得分:-1)

这是一篇讨论您正在做的事情的许多方面的博客: http://mysql.rjweb.org/doc.php/pagination 它还展示了如何做分页&#34;分页&#34;以可扩展的方式。