fetchforo4's avatar

How to do this?

Hi there, For a forum I'm making, I'm working on a search feature that currently searches through users, topics, and posts. I also use pagination within the topic view to get rid of the clutter, and that's what this problem has to do with. So anyway...

Say Bob posts on a topic and there are 5 posts (5 per page). I already have it so Bob gets redirected when he replies, assuming a new page is created. So here's the problem. How do I figure out what page an object within a paginated array is on? (Basically, how can I figure out what page the post is on?)

I don't know if this is possible, but if it is, can someone please help?

0 likes
10 replies
fetchforo4's avatar

@bashy That sorta explains it, but what I need is a way to get what page a certain object is on.

Penderis's avatar

Redirected where? away from the single post he was commenting on to the list? or is he able to comment direct from the listed view?

Penderis's avatar

ok if you are sure it will be the last or only page, I do not know the paginator too well but look into just jumping to the last page by default if not possible go to normal route but with a optional parameter that if set will have the controller return reversed results making the last page the first page. so normal url is maybe my/posts where as this on if set is my/posts/reversed , normal links will still work but redirect can then just add this extra parameter.

But like I said I do not have much experience with that and I would solve it this way at first anyway.

bashy's avatar

Something like this?

$page = ceil(($posts_before + 1) / X); // X = pagination number
1 like
bashy's avatar

It would be current reply ID minus topic ID? I don't know what your DB structure is like.

fetchforo4's avatar

@bashy I just tried that, and I'm getting negative numbers for some reason.

Some info:

  • I'm currently showing 3 posts per page
  • The thread I'm testing on has 3 pages
  • For my search feature, it links to the thread, then adds the page query string, then adds a "fragment" (#post-x)

I have no clue why I'm getting negative numbers.

My code:


    public function getPageNumberAttribute()
    {
        $topic = $this->topic;

        $posts_before = $topic->id - $this->id;

        $page = ceil(($posts_before + 1) / 3);

        return $page;
    }

    public function getRouteAttribute()
    {
        $url = route('forum.get.show.thread', ['slug' => $this->topic->slug, 'id' => $this->topic->id]);

        if ($this->topic->postsPaginated->hasPages())
        {
            $url .= '?page=' . $this->pageNumber;
        }

        $url .= '#post-' . $this->id;

        return $url;
    }

"postsPaginated" is just an object on my Topic model that returns the posts for the topic, but as a Paginator instance.

Any idea what's going on?...

bashy's avatar

Topic ID will be less than the reply ID so you have it the wrong way around?

Please or to participate in this conversation.