duanbi3786 2013-07-26 19:16
浏览 29
已采纳

确定论坛帖子所在的页面

How would one do that?

I have a table called posts which contains the all the posts for certain topics where I show 10 posts per page.

So I got these tables:

id   | title
8976 | Determine which page a post is in

and a posts table

id | author | message | topic_id
1  |  jim   | bla bla | 8976
2  |  jake  | bla bla | 8976
3  |  bob   | bla bla | 8976
4  |  rob   | bla bla | 8976
5  |  john  | bla bla | 8976
6  |  aron  | bla bla | 8976
7  |  eric  | bla bla | 8976
8  |  brad  | bla bla | 8976
9  |  ken   | bla bla | 8976
10 | james  | bla bla | 8976
11 |  dan   | bla bla | 8976
12 |  stan  | bla bla | 8976
13 |  nick  | bla bla | 8976
14 | justin | bla bla | 8976
15 |  steve | bla bla | 8976
16 |  mark  | bla bla | 8976
17 |  billy | bla bla | 8976
18 |  harry | bla bla | 8976
19 |  david | bla bla | 8976
20 |  brian | bla bla | 8976

Why do I wanna do this? To redirect the user to the post they edited. Now if it's on another page then first page, well yeah they wont get redirected to it.

and I'm not gonna use a pre made forum. Im just making this for learning and fun and it will probably never go live.

I know how to count and get the numbers of rows, but then figure out what page it is on I have no idea. I'm terrible at math, and I really mean terrible. So please help me in the right direction.

Edit

Right now I'm using:

$test = $db->query("SELECT COUNT(*) FROM posts WHERE topic_id = $topic_id AND id <= $id")->fetchColumn();

echo "Post is located on page: ".$test[0];

Does work, but the 2 last posts on EVERY page gets current page + 1 :/

and if theres lets say one post on the last page, it says its on page 1

  • 写回答

2条回答 默认 最新

  • dongyun8138 2013-07-26 19:48
    关注

    Here is a mySQL solution to fetch the page number of the post with id=19 . I'm considering 10 itens per page.

    SELECT FLOOR(COUNT(id)/10) + 1
        FROM posts
        WHERE id < 19
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭