简体   繁体   English

如何在MySQL中获取带有帖子ID的帖子页面

[英]how to get post page with post id in MySQL

i wrote a query to get my posts with DESC order as you see its limitet to show 10 posts per page but how i can find the post with "pid = 18" in what page? 我写了一个查询来获取具有DESC顺序的帖子,因为您看到它的极限显示每页10个帖子,但是我如何在哪个页面中找到“ pid = 18”的帖子?

TABLE: 表:

pid, posterid, content
1 , 26 , blabla
2 , 8 , sec balnla
3 , 9 , lollll
4 , 26 , orddddd
5 , 10 , sssssdsd
...

PHP: PHP:

function getComments($poster){
    $sql = "SELECT * FROM table WHERE posterid = $poster"
    $smt = $db->query($sql); 
    $total = $smt->rowCount();

    $result = $db->prepare($sql . " ORDER BY pid DESC $limit");
    $result->execute();
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        $Comments[] = $row;
    }
    return [
        'comments' => $Comments,
        'total' => $total
    ];
}

NEEDED FUNCTION: 所需功能:

$poster = 26;
$pid = 18;
$posttperpage = 10;

public function getPostPage($pid, $poster, $posttperpage)
{
    $page = '';

    $totlaPosts = getComments($poster)['total']; //70

    $pages = $totlaPosts / $posttperpage; //7

    for($i=0, $i<$totlaPosts, $i--)
    {
        // ?????
        // i confuzed no idead what can i do :(

    }
    return $page; //it must be 2
}

Since pid is not necessarily consistent (you might have deleted some rows) you should have another query. 由于pid不一定是一致的(您可能已经删除了一些行),因此您应该有另一个查询。

$stmt = $db->prepare("SELECT COUNT(1) FROM table WHERE pid <= ? AND poster = ?");
$stmt->execute([$pid, $poster]);
$count = $stmt->fetchColumn();

echo ceil($count / $posttperpage);

If you're looking to determine what page a post is on: 如果您要确定帖子在哪个页面上:

$k = 1;
for($i=0, $i<$pid; $i += $posttperpage){
    $page = $k;
    $k +=1;
}

//after loop $page now contains what page the post is on. 

Is this what you're looking for? 这是您要找的东西吗?

In your LIMIT 0,10 you have a range of 10 items per page, right? 在您的LIMIT 0,10 ,每页有10个项目,对吗? If you want to keep track of which page you are viewing you need a variable that behaves like this: 如果要跟踪正在查看的页面,则需要一个行为如下的变量:

$range = 10;
$currentPage = 2;
$offset = $range * ($currentPage - 1);

Inside your query you can change that 0 to $offset and there you have it, if you find your result in with this query it means your pid is in page 2, you can also increase the $currentPage by 1 if you can't find your result and so on. 在您的查询中,您可以将0更改为$offset ,如果您在此查询中找到结果,则表示您的pid在第2页中,如果找不到,您也可以将$currentPage加1您的结果等等。

This algo is simple, 这个算法很简单,

You need to know the total of row, the position of your specific post and number of post per page. 您需要知道总行数,特定帖子的位置以及每页的帖子数。

And you do like this : 而且您确实这样:

floor(position_of_you_post / number_total_of_post * number_of_post_per_page) 楼层(职位的职位数/职位的总数*职位的每页数)

so if you have 100 post with 10 post per page and your post is the number 51, you can do : 因此,如果您有100个帖子,每页10个帖子,而您的帖子是51个帖子,则可以执行以下操作:

51 /100 * 10 = 5.1 51/100 * 10 = 5.1

so the post is on the page 5 所以帖子在第5页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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