Jump to content

Recommended Posts

Hi all. Have another headache trying to figure this out.

Im trying to select all posts from a table, and echo each post out BUT I only want to echo out say 10 posts per page, then generate a new page to list the next 10. The only problem is I want to echo the newest posts first so that the new post is always on top and the old messages get sent back. Also I cant work out how to stop generating new pages once the posts have run out.

 

Basically the way I have it working at the moment it does this:

 

1) Posts get listed in date order but the newest post goes to bottom of page.

2) When I generate a new page I end up running the sql statement from the beggining again so I end up repeating the same messages on the previous page.

 

I know what I need to use but its missing a couple of things to work how I want it to.

 

      
$page = $_GET['next_page'];

$conn = db_connect();
      $result1 = $conn->query("SELECT COUNT(*) FROM posts WHERE username IS NULL AND clientof = '$user'");
      $result2 = $conn->query("SELECT * FROM posts WHERE username IS NULL AND clientof = '$user' ORDER BY sent ASC");
      $rows = $result1->fetchColumn();
      $message = $result2->fetchObject();

  if(!isset($page))
  {
    for($i = 0; $i < 11; $i++)
    {
       echo $message->post;
     }
     echo '<a href="this_page.php?nextpage=2">Next Page</a>';
  }

     if($page == 2)
     {
        for($i = 0; $i < 11; $i++)
        {
           echo $message->post;
         }
         echo '<a href="this_page.php?nextpage=3">Next Page</a>';
      }

     if($page == 3)
     {
        for($i = 0; $i < 11; $i++)
        {
           echo $message->post;
         }
         echo '<a href="this_page.php?nextpage=4">Next Page</a>';
      }
          

 

Its obvoius that this is just a never ending cycle and I am limiting the number of pages when it should be dynamic. So i need to use some kind of loop to find the end of the posts, trouble is my brain cant figure out how to do this. Any help greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/150312-solved-displaying-forum-posts/
Share on other sites

ah i just thought of somthing...

I find out how many rows are in the table, divide by 10 and round it up to nearest unit. This will tell me how many pages I require and can work from that.

Im gna try to get something out of that, but I would still appreciate help. Thanks.

firstly the order.  That can be fixed by ORDER BY whateverthecolumnis DESC  in your mySql. That will change the order to the opposite of the default.

 

Next: logic.

Pagination is not as easy as it looks.  Try this: http://php.about.com/od/phpwithmysql/ss/php_pagination.htm

well you need to use the LIMIT clause in the query

seach MySQL manual for the LIMIT clause

 

i've made a class already to paginate, if you'd like to have it message me.

 

and do some equation to calculate the LIMIT's offest and count from the page number

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.