globetrottingmike Posted September 6, 2008 Share Posted September 6, 2008 Hi All, What would be the best way to create links for a results page which is paginated, but the page number is unknown? An example would be that there are 50 results with 10 reults page, so pagination would show 5 pages of results. That bit is fine. When I go to add a new record and the ask the insirt record form to redirect to this page, it will need to know that the 51st record is at the top of page 6. I know this can be done as other sites have this set up. PunBB forums for example have a normal paginated page result of www.site.com/viewtopic.php?id=6298&p=2 (where id= the topic id and p= the paginated page number of the results). But when the paginated page result in unknown the us the url of: www.site.com/viewtopic.php?pid=82811#p82811 (where pid= the post id). So they work out the paginated page fromt he post id. Does anyone have pagination experience who can try to help me replicate this. Thanks in advance for any help anyone can give. Mike Quote Link to comment https://forums.phpfreaks.com/topic/122992-recordset-navigation/ Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 Here is a sample pagination script... it is fairly lengthy. I have tried to comment the hell out of it so you can see exactly what everything is doing. Looks like some of the code indentation and such is not going to go through correctly, so I have included a txt file that has this script in it. I have modified it a good deal from the original to simplify the code in it. You can see the working example at http://millstarentertainment.com/browse.php This site is unfinished, but the browse library works. This is my cousins karaoke library. Enjoy! <?php // make a db connection here // determine how many results are going to be returned. $result = mysql_query("SELECT count(*) as total FROM tracks") or die(mysql_error()); while($row=mysql_fetch_object($result)) { $total_entries = $row->total; // set our total entries var } $setlimit = 10; // how many per page are we going to show if(isset($_GET['page'])) // is page already set? { $page = $_GET['page']; // it is, so set $page to the $_GET['page'] var } else { // else its not set, so set page to 1 $page = 1; } $pages = ceil($total_entries / $setlimit); // calculate the number of pages needed $offset = ($page - 1) * $setlimit; // determine the offset based on our limit and our page number $result = mysql_query("SELECT * FROM tracks LIMIT $offset, $setlimit "); // make our query with our starting row and our limit while($row = mysql_fetch_object($result)) { $track[] = 'fake array here'; // a fake array I set up in place of the lengthy code that was here }; ?> <table width="90%" align="center" cellpadding="5" cellspacing="0"> <?php $x=0; // initalize a counter $row_count = "0"; // set our row count while($x < count($track)) { // loop through the results and display them ?> <tr align="left" valign="middle"><td height="20" ><?php echo $track[$x++] ?></td> <td height="20" ><?php echo $track[$x++] ?></td> </tr> <?php $row_count++; // increment our row counter } ?> </table> <br> <?php echo '<center>'; if($pages >1) // if we have more than 1 page to show, then we are going to show the pagination links { if($page!=1) // if we are not on page 1 { echo '<a href="'.$PHP_SELF.'?page=1 "> << </a> '; // show first arrow echo '<a href="'.$PHP_SELF.'?page='.($page-1).' "> < </a> '; // show previous arrow } else { // we are on page 1 so no need to show the prev or first link } for($i = ($page - 2); $i <= ($page + 2); $i++) // set i to current page minus 2 and stop at current page plus 2 { // here we are setting up links for 2 previous to the current page // so if we are on page 5 it will show as << < 3 4 5 6 7 > >> if (($i >= 1) && ($i <= $pages)) // if $i is greater than or equal to 1 and less than or equal to the number of pages // then we are going to print the links... this prevents us from printing page numbers that don't exist { echo '<span >'.$i.'</span> '; } // end if } // end for if($page != $pages) // if we are not on the last page { echo '<a href="'.$PHP_SELF.'?page='.($page+1).'" > > </a> '; // show next link echo '<a href="'.$PHP_SELF.'?page='.$pages .'" > >> </a> '; // show last link } else { //don't show next or last link }; };//end if $pages > 1 ?> Nate [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/122992-recordset-navigation/#findComment-646269 Share on other sites More sharing options...
globetrottingmike Posted September 20, 2008 Author Share Posted September 20, 2008 Thanks Chronister for your reply. What I need to do is be able to go to a paginated page that is worked out by the record_id field. PunBB use the following piece of code in their viewtopic.php page to determine the page number is such a way. // If a post ID is specified we determine topic ID and page number so we can redirect to the correct message if ($pid) { $result = $db->query('SELECT topic_id FROM '.$db->prefix.'posts WHERE id='.$pid) or error('Unable to fetch post info', __FILE__, __LINE__, $db->error()); if (!$db->num_rows($result)) message($lang_common['Bad request']); $id = $db->result($result); // Determine on what page the post is located (depending on $pun_user['disp_posts']) $result = $db->query('SELECT id FROM '.$db->prefix.'posts WHERE topic_id='.$id.' ORDER BY posted') or error('Unable to fetch post info', __FILE__, __LINE__, $db->error()); $num_posts = $db->num_rows($result); for ($i = 0; $i < $num_posts; ++$i) { $cur_id = $db->result($result, $i); if ($cur_id == $pid) break; } ++$i; // we started at 0 $_GET['p'] = ceil($i / $pun_user['disp_posts']); } The would give a URL like www.mysite.com/somepage.php?id=1233#id1234 Do you know how this would integrate with your code? Let me know if you would like a clearer explanation. Quote Link to comment https://forums.phpfreaks.com/topic/122992-recordset-navigation/#findComment-646289 Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 I think a better approach would be to look over what I gave you and learn the concept of it and what it is doing and then determine how to apply those concepts to what your doing. I originally had a sortby clause in there so it can be sorted by letter and still be paginated. So you just need to basically pass the id into it and then apply the pagination concept to that by determining how many posts you wish to show on each page. Nate Quote Link to comment https://forums.phpfreaks.com/topic/122992-recordset-navigation/#findComment-646493 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.