fry2010 Posted March 20, 2009 Share Posted March 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150312-solved-displaying-forum-posts/ Share on other sites More sharing options...
fry2010 Posted March 20, 2009 Author Share Posted March 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150312-solved-displaying-forum-posts/#findComment-789440 Share on other sites More sharing options...
defeated Posted March 20, 2009 Share Posted March 20, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/150312-solved-displaying-forum-posts/#findComment-789442 Share on other sites More sharing options...
defeated Posted March 20, 2009 Share Posted March 20, 2009 that sounds like you're on the right track! Quote Link to comment https://forums.phpfreaks.com/topic/150312-solved-displaying-forum-posts/#findComment-789444 Share on other sites More sharing options...
BloodyMind Posted March 20, 2009 Share Posted March 20, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/150312-solved-displaying-forum-posts/#findComment-789446 Share on other sites More sharing options...
fry2010 Posted March 20, 2009 Author Share Posted March 20, 2009 thank you very much guys, I have plenty to read through now. Bloodymind thanks for the limit clause thing I just look at that and came across another great article. Cant believe I over looked DESC too.... nvm. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/150312-solved-displaying-forum-posts/#findComment-789455 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.