0

I'm making a little forum just for fun and practice probably never gonna put it in action, I'm not a real php guru it's just a hobby of mine. Right now I'm working on the edit post function. Everything is perfect except one thing I'm trying to figure out and that is how to redirect the user to his forum post after I *he have edited it. Like all the professional forums.

I show 10 posts per page...

Anyways this is how far I have come. Now I know how many pages the topic has but the thing I'm trying to figure out is how determine which page the specific post is on. And to be honest I have no idea. Tried google but I can't find anything helpful. Don't really know what to search for.

I sort posts by date BTW. Please let me know if you need any more info. Hopefully someone can point me in the right direction. Perhaps you have a better idea how to redirect to the post. I'm gonna look into PDO soon :) Please don't hate.

editpost.php

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// Fetch some info about the topic and/or the forum
$query = mysqli_query($link, "SELECT * FROM posts
                              WHERE id = $id");

// Someone pressed "Edit Post"
if (count($_POST) > 0) {
    // validate stuff
    update post...
}

// See if there is more than one pages so we can redirect to the correct message, how we are gonna know which page the post is on i have no idea
$totalCount = "SELECT COUNT(*) as 'Total' FROM posts WHERE topic_id = $row->topic_id";
$rsCount = mysqli_query($link, $totalCount) or die(mysqli_error($link));
$rowCount = mysqli_fetch_object($rsCount);
$numOfPages = ceil($rowCount->Total / 10);

// just now during development to see if it counted correct
// there is more than 10 posts so we must determine which page this post belongs on
if ($rowCount->Total > 10)
echo "Page: $numOfPages";

header("Location thread.php?id=$id&page=???#post$id");

The tables look like this:

topics
id | subject | postdate | forum_id

posts
id | poster_id | message | postdate | topic_id
Barmar
  • 741,623
  • 53
  • 500
  • 612
Johnny
  • 23
  • 2
  • possible duplicate of [Mysql rank function](http://stackoverflow.com/questions/3333665/mysql-rank-function) – Barmar Jun 07 '13 at 01:00
  • You just need to find the rank, and divide by the number or posts per page to get the page number. See the linked question. – Barmar Jun 07 '13 at 01:01

2 Answers2

0
$pagenumber = intval($postnumber / 10); //assuming 10 posts per page
Bollis
  • 385
  • 1
  • 11
0

I got it.

$totalCount = mysqli_query($link, "SELECT COUNT(*) as 'Total' FROM posts WHERE topic_id =           $row->topic_id AND posted < $row->posted");
$rowCount = mysqli_fetch_object($totalCount);
$page = ceil($rowCount->Total / 9);
$redirect = ($page > 1) ? "&page=$page#post$id" : "#post$id";
Johnny
  • 23
  • 2