简体   繁体   中英

Previous or Next 10 links for search results page

I am trying to create a PDO database results page with a previous and next for the previous 10 or next 10 records. I just can't figure out what should go in the 4 ????? areas below. I have tried several different approaches with all sorts of incorrect results. Any ideas on the missing pieces to make this work?

$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';
}

这是一个讨论您正在做的事情的许多博客: http : //mysql.rjweb.org/doc.php/pagination它还显示了如何以可扩展的方式进行“分页”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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