【问题标题】:How to link to the right page of a topic in a search result?如何链接到搜索结果中某个主题的正确页面?
【发布时间】: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'] . '&amp;cat_id=' . $row['topic_cat'] . '
        &amp;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


【解决方案1】:

假设你只知道帖子的 ID,我会这样:

SELECT
    count(*)
FROM
    `posts`
WHERE
    date <= ( SELECT date FROM `posts` WHERE post_id = '$id' )
ORDER BY
    date DESC;

这将为您提供此帖子的行数。之后,只需执行一些 php 代码,例如:

$start = floor( $nr / 10 ) * 10;
$end = ceil( $nr / 10 ) * 10;

对于多个 ID:

SELECT topic_id, (
    SELECT
        count(*)
    FROM
        `posts`
    WHERE
        date <= ( SELECT date FROM `posts` u1 WHERE u1.topic_id = u2.topic_id )
    ) AS row
FROM
    `posts` u2
WHERE
    topic_id IN ( '$id1', '$id2', '$id3' )
ORDER BY
    date DESC;

【讨论】:

  • 我认为它一定是 WHERE topic_id = '$topic_id'?因为我需要该主题中该帖子的编号。但是我不是必须对每个搜索结果都进行该查询吗?
  • 虽然你只需要一个,但我会在一分钟内重写。
  • 好的。搜索结果与 PDO 一起显示:while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { } 所以如果能够以某种方式在每个结果集中包含正确的开始参数,只需合并一些原始查询中的额外选择。
  • @NewInTheBusiness 已编辑答案。抱歉,m8,这是我能想到的最好的方法,但我相信还有更好的方法。
【解决方案2】:
$query = 'SELECT :start as start, 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';

然后通过$start = $row['start']访问

$nr = count($array[$row['topic_subject']])+$start - 1;

【讨论】:

    【解决方案3】:

    正如你所说,我遇到了问题。我认为此链接对您有用。

    我已经成功完成了这些,希望对你有用: 1.http://www.awcore.com/dev/1/3/Create-Awesome-PHPMYSQL-Pagination_en 2.http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多