【发布时间】:2012-09-04 07:46:33
【问题描述】:
我在一个论坛里有一个搜索功能,搜索结果会在当时显示10。然后,用户可以查看下一个或前 10 个搜索结果。结果显示要在其中找到搜索词的不同主题。一切都如我所愿。
问题是当我希望用户能够单击结果并最终进入该主题的正确页面时。例如,必须在第 2 页查看某个主题的第 14 条帖子(在主题页面的 SQL 查询中使用 LIMIT 10,10)。我在链接中将LIMIT 参数作为$_GET 发送。
按发布日期排序时,如何从该特定主题的总数中检索结果中每个主题的行号?一切都始终按该顺序显示。我想用$nr = $nr-1; //and then
$limit = floor($nr / 10) * 10;
在该号码上能够发送正确的LIMIT 参数以及搜索结果中的链接。
这是用于获取搜索结果的 PDO:
$query = 'SELECT t.topic_id, t.topic_cat, t.topic_subject, p.post_content, p.post_id, UNIX_TIMESTAMP(p.post_date) AS post_date
FROM posts p
LEFT JOIN topics t ON p.post_topic = t.topic_id
WHERE p.post_content LIKE :search OR t.topic_subject LIKE :search ORDER BY UNIX_TIMESTAMP(p.post_date) DESC LIMIT :start, :size';
$statement = $db->prepare($query);
$statement->bindValue(':search', '%'.$search.'%');
$statement->bindValue(':start', $start2, PDO::PARAM_INT);
$statement->bindValue(':size', $pagesize, PDO::PARAM_INT);
$statement->execute();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$array[$row['topic_subject']][] = $row['post_id'];
$nr = count($array[$row['topic_subject']]) - 1;
echo '<div class="search_result"><div class="search_topic"><a href="topic_view.php?id=' . $row['topic_id'] . '&cat_id=' . $row['topic_cat'] . '
&start=' . floor($nr / 10) * 10 . '#anchor_' . $row['post_id'] . '">' . $row['topic_subject'] . '</a><span style="float:right;color:#696969">'
. date("M d, Y",$row['post_date']) . '</span></div><div class="search_post">' . $row['post_content'] . '</div></div>';
}
} $statement->closeCursor();
链接中的start 参数是我需要在查询中获取的,因此我不必为while loop 中的每个post_id 进行新的数据库调用。
【问题讨论】:
-
我不明白这个问题。你有行号,你想知道它在哪一页上?
-
我有搜索结果中显示的每个帖子的 ID(每个结果的标题是主题),但是为了能够将用户发送到该主题的正确页面,我需要知道在 LIMIT 子句中使用什么数字。
标签: php sql search pagination