MasterACE14 Posted October 21, 2007 Share Posted October 21, 2007 Evening Everyone, I have integrated the Pagination script from one of the tutorials here, and it is working fine, except for the links are alittle muddled up. What it does, is limit the amount of results echo'd to the page from my database, that works fine, creates a new page for each list, and creates the number of the page at the bottom(thus the pagination). So it looks like this: PREV 1 2 3 4 5 NEXT But, the PREV link and NEXT link are reversed, PREV goes to the next page, and NEXT goes to the previous page. And the number '1' never seems to be a link no matter what page you are on. So with my current script, what needs to be changed and modded to correct these problems? pagination script: <?php if($vpage != 1){ $pageprev = $vpage--; echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$pageprev\">PREV</a> "); }else{ echo("PREV "); } $numofpages = $totalrows / $limit; for($i = 1; $i <= $numofpages; $i++){ if($i == $vpage){ echo($i." "); }else{ echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$i\">$i</a> "); } } if(($totalrows % $limit) != 0){ if($i == $vpage){ echo($i." "); }else{ echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$i\">$i</a> "); } } if(($totalrows - ($limit * $vpage)) > 0){ $pagenext = $vpage++; echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$pagenext\">NEXT</a> "); }else{ echo("NEXT "); } mysql_free_result($result); ?> Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/74177-solved-page-pagination-is-stuffed-next-goes-to-prev-and-prev-goes-to-next-etc/ Share on other sites More sharing options...
bozebo Posted October 21, 2007 Share Posted October 21, 2007 Sorry to be lazy at the moment, but maby If I give you a refined version of my own pagignation you can sort yours out: $cards_per_page = 10; if ($_GET['page'] > 0){ $page_number = $_GET['page']; } else { $page_number = 1; } $cardsq = 'SELECT * FROM tblCards'; $cards_data = mysql_query($cardsq) or die(mysql_error()); $pages = ceil(mysql_num_rows($cards_data) / $cards_per_page); if ($page_number > $pages){ $page_number = $pages; } $p_start = ($page_number * $cards_per_page) - $cards_per_page; $cardsq = 'SELECT * FROM tblCards LIMIT '. $p_start .', '. $cards_per_page; $cards_data = mysql_query($cardsq) or die(mysql_error() .' Query:<br />'. $cardsq ); if ($pages > 1){ echo '<p>Page:<br />'; for($i = 1; $i <= $pages; $i++){ if ($page_number != $i){ echo ' <a href="?action=lookup_card&page='. (string)$i .'">'. (string)$i .'</a>'; } else { echo ' '. (string)$i; } echo '</p>'; } } while($card = mysql_fetch_assoc($cards_data)){ //do something for each record that is on this page } if ($pages > 1){ echo '<p>Page:<br />'; for($i = 1; $i <= $pages; $i++){ if ($page_number != $i){ echo ' <a href="?action=lookup_card&page='. (string)$i .'">'. (string)$i .'</a>'; } else { echo ' '. (string)$i; } } echo '</p>'; }; Quote Link to comment https://forums.phpfreaks.com/topic/74177-solved-page-pagination-is-stuffed-next-goes-to-prev-and-prev-goes-to-next-etc/#findComment-374648 Share on other sites More sharing options...
MasterACE14 Posted October 21, 2007 Author Share Posted October 21, 2007 ... I'll wait lol thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/74177-solved-page-pagination-is-stuffed-next-goes-to-prev-and-prev-goes-to-next-etc/#findComment-374653 Share on other sites More sharing options...
kratsg Posted October 21, 2007 Share Posted October 21, 2007 Hmm, I didn't like that pagination script, kinda crapped my script when I used it before. Try this one, simpler, and a bit better: <?php // Database Connection include 'db.php'; // If current page number, use it // if not, set one! if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } // Define the number of results per page $max_results = 10; // Figure out the limit for the query based // on the current page number. $from = (($page * $max_results) - $max_results); // Perform MySQL query on only the current page number's results $sql = mysql_query("SELECT * FROM pages LIMIT $from, $max_results"); while($row = mysql_fetch_array($sql)){ // Build your formatted results here. echo $row['title']."<br />"; } // Figure out the total number of results in DB: $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pages"),0); // Figure out the total number of pages. Always round up using ceil() $total_pages = ceil($total_results / $max_results); // Build Page Number Hyperlinks echo "<center>Select a Page<br />"; // Build Previous Link if($page > 1){ $prev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> "; } for($i = 1; $i <= $total_pages; $i++){ if(($page) == $i){ echo "$i "; } else { echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } } // Build Next Link if($page < $total_pages){ $next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>"; } echo "</center>"; ?> It basically auto-defines those links for you. The following pieces need to be changed, and that's it: // Database Connection include 'db.php'; // Define the number of results per page $max_results = 10; // Perform MySQL query on only the current page number's results $sql = mysql_query("SELECT * FROM pages LIMIT $from, $max_results"); //Keep the LIMIT $from, $max_results in the end, it's needed to limit number of results per page while($row = mysql_fetch_array($sql)){ // Build your formatted results here. echo $row['title']."<br />"; } http://www.phpfreaks.com/tutorials/73/1.php Why do I prefer this? I can keep it in a class, so I can run it with a given query, results per page, and formatted results, and it will run through it all :-) It's nice. Let me know if this one works off the bat. Quote Link to comment https://forums.phpfreaks.com/topic/74177-solved-page-pagination-is-stuffed-next-goes-to-prev-and-prev-goes-to-next-etc/#findComment-374720 Share on other sites More sharing options...
MasterACE14 Posted October 22, 2007 Author Share Posted October 22, 2007 I don't know if I can use this script as I use a switch statement to set $_GET["page"]. Quote Link to comment https://forums.phpfreaks.com/topic/74177-solved-page-pagination-is-stuffed-next-goes-to-prev-and-prev-goes-to-next-etc/#findComment-375338 Share on other sites More sharing options...
sasa Posted October 22, 2007 Share Posted October 22, 2007 change: $pageprev = $vpage--; to $pageprev = $vpage-1; and $pagenext = $vpage++; to $pagenext = $vpage+1; Quote Link to comment https://forums.phpfreaks.com/topic/74177-solved-page-pagination-is-stuffed-next-goes-to-prev-and-prev-goes-to-next-etc/#findComment-375431 Share on other sites More sharing options...
chocopi Posted October 22, 2007 Share Posted October 22, 2007 I know you are just trying to fix your code, but I was messing around with a mixture of codes and ended up with this: <?php $base_url = $_SERVER['PHP_SELF']; // this is just to get the page your on $num_items = 121; // this is the total amount of results $per_page = 5; // this is obviously the amount of results you want on each page $start_item = (empty($_GET['start'])) ? 1 : $_GET['start']; // this is the get to get where to start function pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE) { $begin_end = 3; // this is how many numbers will be placed at the beggining ie 1,2,3 ... 8,9,10, if 5 then 1,2,3,4,5 .... 11,12,13,14,15 etc $from_middle = 1; // how many results wil be displayed in middle ie for 1 ... 7,8,9 ... if 3 then 7,8,9,10,11,12,13 $total_pages = ceil($num_items/$per_page); if ( $total_pages == 1 ) { return ''; } $on_page = floor($start_item / $per_page) + 1; $page_string = ''; if ( $total_pages > ((2*($begin_end + $from_middle)) + 2) ) { $init_page_max = ( $total_pages > $begin_end ) ? $begin_end : $total_pages; for($i = 1; $i < $init_page_max + 1; $i++) { $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' .$base_url . "?start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>'; if ( $i < $init_page_max ) { $page_string .= ", "; } } if ( $total_pages > $begin_end ) { if ( $on_page > 1 && $on_page < $total_pages ) { $page_string .= ( $on_page > ($begin_end + $from_middle + 1) ) ? ' ... ' : ', '; $init_page_min = ( $on_page > ($begin_end + $from_middle) ) ? $on_page : ($begin_end + $from_middle + 1); $init_page_max = ( $on_page < $total_pages - ($begin_end + $from_middle) ) ? $on_page : $total_pages - ($begin_end + $from_middle); for($i = $init_page_min - $from_middle; $i < $init_page_max + ($from_middle + 1); $i++) { $page_string .= ($i == $on_page) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "?start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>'; if ( $i < $init_page_max + $from_middle ) { $page_string .= ', '; } } $page_string .= ( $on_page < $total_pages - ($begin_end + $from_middle) ) ? ' ... ' : ', '; } else { $page_string .= ' ... '; } for($i = $total_pages - ($begin_end - 1); $i < $total_pages + 1; $i++) { $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "?start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>'; if( $i < $total_pages ) { $page_string .= ", "; } } } } else { for($i = 1; $i < $total_pages + 1; $i++) { $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "?start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>'; if ( $i < $total_pages ) { $page_string .= ', '; } } } if ( $add_prevnext_text ) { if ( $on_page > 1 ) { $page_string = ' <a href="' . $base_url . "?start=" . ( ( $on_page - 2 ) * $per_page ) . '">Previous</a> ' . $page_string; } if ( $on_page < $total_pages ) { $page_string .= ' <a href="' . $base_url . "?start=" . ( $on_page * $per_page ) . '">Next</a>'; } } $page_string = 'Go To ' . $page_string; return $page_string; } echo pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE); // set to false if you dont want next and prev link ?> I haven't found any problems with it yet So I thought I would throw this out there as it is nearly impossible to find a good pagination script. Well I hope someone finds it useful ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/74177-solved-page-pagination-is-stuffed-next-goes-to-prev-and-prev-goes-to-next-etc/#findComment-375474 Share on other sites More sharing options...
MasterACE14 Posted October 23, 2007 Author Share Posted October 23, 2007 change: $pageprev = $vpage--; to $pageprev = $vpage-1; and $pagenext = $vpage++; to $pagenext = $vpage+1; Thats done the trick , thank you so much , I can't believe it was that simple lol. Also thanks to everyone else. We've got a nice variety of Pagination scripts here now lol. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/74177-solved-page-pagination-is-stuffed-next-goes-to-prev-and-prev-goes-to-next-etc/#findComment-375993 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.