Hello!
I didn't knew where to post this so i posted it here.
I am trying to make messages limit.e.g. i am having 100 messages in database and i want to display 10 messages per page and the next page will appear below the last post.Just like here in Daniweb.com.But i don't know how to do this please help me.

Recommended Answers

All 4 Replies

  1. Get the total number of messages per thread.
  2. Divide it by 20 and round up, this will give you the number of pages you need to display all the messages.
  3. Declare a variable to hold the number of messages already received for the current page (for example: $msg_count).

So basically you'll need something like this:

// While there are pages left
while($pages_to_write > 0)
{
    $msg_count = 0;
    $pages_to_write--;
    
    // While there are messages left, and the page isn't full
    while($msg_left > 0 && $msg_count < 20)
    {
        // Write next message to page
        write_msg_to_page( next_msg() );
        
        $msg_count++;
        $msg_left--;
    }
}

(please note that this is some kind of pseudo-code, this means that you'll have to write the routines yourself)

I assume that the next_msg() function returns an object which contains all data for the message, and that the write_msg_to_page() function takes such an object as input.
You'll have to implement both functions yourself, and you'll also have to design the 'message'-class.

The code is great.Your good at it.
But sorry for not clearing the question.Actually i want a pagination system that gets messages from database and displays 20 per page.And the newst at the top of the page. like in the forums for threads.

What I would do is write the next_msg() function in such a way that it always returns the newest post.
The newest post is always the one with the highest primary ID/key in the database, so you could maybe order them in a descend way (in that way, the posts are sorted from latest to first).
Now you only have to save the result of the query and each time return the next post.

I am trying to make messages limit.e.g. i am having 100 messages in database and i want to display 10 messages per page and the next page will appear below the last post.

Whoops! I must have misread that, I meant to have read

20 messages per page

So, make sure you change it in that small code I provided you with (as well as in the explanation: "divide it by 20....")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.