3

我在一个论坛里有一个搜索功能,搜索结果会在当时显示10。然后,用户可以查看下一个或前 10 个搜索结果。结果显示要在其中找到搜索词的不同主题。一切都如我所愿。

问题是当我希望用户能够单击结果并最终进入该主题的正确页面时。例如,必须在第 2 页上查看某个主题中的第 14 条帖子(LIMIT 10,10在主题页面上的 SQL 查询中使用)。我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链接中的参数,我需要以某种方式在查询中获取,因此我不必post_idwhile loop.

4

3 回答 3

3

假设你只知道帖子的 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;
于 2012-09-11T11:34:16.137 回答
1
$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;
于 2012-09-19T19:17:04.353 回答
0

我遇到了你说的问题。我认为此链接对您有用。

我已经成功完成了这些,希望对你有用: 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

于 2012-09-20T07:28:48.697 回答