chocopi Posted June 23, 2007 Share Posted June 23, 2007 Can someone help me with my pagination ? I have used it fine before but i do not know how to impliment it on my current page. I have a page that loops throught my database results and I want a new page to be created every 10 results. The page number is pulled from the url using $_GET so I was wondering if anyone could possible help. Here is the code <?php // start session session_start(); require_once('page_header.php'); // get board id $board = $_GET['board']; if(empty($board)) { // set board id $board = '1'; } else if(!empty($board)) { // set board to session $_SESSION['board'] = $board; } // get page number $page_num = $_GET['page']; // get max post_num $query = mysql_query("SELECT MAX(post_num) AS `post_num_max` FROM `zBoard_messages` WHERE board_id='$board'") or die(mysql_error()); $fetch= mysql_fetch_assoc($query) or die(mysql_error()); $post_num_max = $fetch['post_num_max']; for ($pn = 1; $pn <= $post_num_max; $pn++) { // some loopy stuf } ?> So I need something like $pn divide by 10 then round up to nearest interger which would then decide where the info goes. Many thanks ~ Chocopi Quote Link to comment Share on other sites More sharing options...
AndyB Posted June 23, 2007 Share Posted June 23, 2007 So I need something like $pn divide by 10 then round up to nearest interger which would then decide where the info goes. $whatever = ceil($pn/10); Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 23, 2007 Author Share Posted June 23, 2007 Cheers, but then how do I then make it so that the links are created on the page like: <prev 1, 2, 3, next> ~ Chocopi Quote Link to comment Share on other sites More sharing options...
spooke2k Posted June 23, 2007 Share Posted June 23, 2007 seriously m8 look at the tutorials on this site someone has gone through great lengths to write one on pagination and give a working demo that does exactly what u want if your stuck after tutorial then come back with bit ur stuck on ps do a search on here for it and theres like 30 posts Quote Link to comment Share on other sites More sharing options...
spooke2k Posted June 23, 2007 Share Posted June 23, 2007 http://www.phpfreaks.com/quickcode_cats/33/Pagination.php demos showing to use use it Spooke2k Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 24, 2007 Author Share Posted June 24, 2007 I have had a look at them before, but i didnt really like them So i took a stab at it and it works (well i havent come across any problems yet) here is the code i wrote, thanks for your help aswell ! <?php // start session session_start(); require_once('page_header.php'); // get board id $board = $_GET['board']; if(empty($board)) { // set board id $board = '1'; } else if(!empty($board)) { // set board to session $_SESSION['board'] = $board; } // get page number $page_num = $_GET['page']; if(empty($page_num)) { //set page num $page_num = '1'; } // get max post_num $query = mysql_query("SELECT MAX(post_num) AS `post_num_max` FROM `zBoard_messages` WHERE board_id='$board'") or die(mysql_error()); $fetch= mysql_fetch_assoc($query) or die(mysql_error()); $post_num_max = $fetch['post_num_max']; for ($pn = 1; $pn <= $post_num_max; $pn++) { // pagination $ppp = 10; //posts per page $page = ceil($pn/$ppp); // get max page $max_page = ceil($post_num_max/$ppp); // if the page equals the page number then print results if($page == $page_num) { // display stuff here } } //get prev button if($page_num == 1) { echo "Prev "; } else if($page_num >=2) { $prev_page = $page_num - 1; echo "<a href=\"view.php?page=".$prev_page."\">Prev</a> "; } // get page numbers for ($page_number = 1; $page_number <= $max_page; $page_number++) { if($page_number == $page_num) { echo $page_number; } else if($page_number != $page_num) { echo "<a href=\"view.php?page=".$page_number."\">".$page_number."</a>"; } if($page_number != $max_page) { echo ", "; } else if($page_number == $max_page) { echo" "; } } // get next button if($page_num == $max_page) { echo " Next"; } else if($page_num < $max_page) { $next_page = $page_num + 1; echo " <a href=\"view.php?page=".$next_page."\">Next</a>"; } require('page_footer.php'); ?> Tell me what you think ~ Chocopi Quote Link to comment 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.