russia5 Posted January 7, 2007 Share Posted January 7, 2007 I am using the code in the tutorial for pagination. I nearly have it working, except the anchors are not working. The PREVIOUS 123 NEXT are appearing, but they are not linked to anything, just plain text. Can anyone see what I need to change. Thanks for the time[code] if ($count == $girlsPerRaw) { $rows++; ?></td></tr><?php } } if($page != 1){ $pageprev = $page--; echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV".$limit."</a> "); }else{ echo("PREV".$limit." "); } $numofpages = $totalrows / $limit; for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } } if(($totalrows % $limit) != 0){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } } if(($totalrows - ($limit * $page)) > 0){ $pagenext = $page++; echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); }else{ echo("NEXT".$limit); } mysql_free_result($result); ?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/33231-pagination/ Share on other sites More sharing options...
.josh Posted January 7, 2007 Share Posted January 7, 2007 well, the only way I see that happening is that you only have one page of results. If you only have 1 page of results, then it's doing exactly what it's supposed to be doing. Quote Link to comment https://forums.phpfreaks.com/topic/33231-pagination/#findComment-155134 Share on other sites More sharing options...
russia5 Posted January 7, 2007 Author Share Posted January 7, 2007 Thanks for your time and advice. I have about 15 pages that should be displayed. Statically, I can get 15. Do you think possibly, that the link paths have a generic problem of some sort. I am not to familiar with using PHP_SELF could that be it? Quote Link to comment https://forums.phpfreaks.com/topic/33231-pagination/#findComment-155305 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 Here's what I use:[code]function paginate($numMatches, $display, $pg, $total) { /* make sure pagination doesn't interfere with other query string variables */ if(isset($_SERVER['QUERY_STRING']) && trim( $_SERVER['QUERY_STRING']) != '') { if(stristr($_SERVER['QUERY_STRING'], 'pg=')) $query_str = '?'.preg_replace('/pg=\d+/', 'pg=', $_SERVER['QUERY_STRING']); else $query_str = '?'.$_SERVER['QUERY_STRING'].'&pg='; } else $query_str = '?pg='; /* find out how many pages we have */ $pages = ($total <= $display) ? 1 : ceil($total / $display); /* create the links */ $first = '<a href="'.$_SERVER['PHP_SELF'].$query_str.'1">First</a>'; $prev = '<a href="'.$_SERVER['PHP_SELF'].$query_str.($pg - 1).'">Previous</a>'; $next = '<a href="'.$_SERVER['PHP_SELF'].$query_str.($pg + 1).'">Next</a>'; $last = '<a href="'.$_SERVER['PHP_SELF'].$query_str.$pages.'">Last</a>'; /* display opening navigation */ echo "<div id=\"searchLinks\" style=\"background:#131aa0;a:link:color:#FFFFFF;\"><center><font color=\"white\"><font size=\"4\"><br>Search Results:</font><br /><br /></font></center><div style=\"background:#0057FC; border:2px solid #000000;font-size:11px;\"><table border=\"1\"><tr><td align=\"center\" width=\"10%\"><font size=\"1\" color=\"white\"><b>$numMatches Matches Found.</b></font></td><td align=\"center\" width=\"10%\"><font size=\"1\" color=\"white\"><b>"; echo ($pg > 1) ? "$first $prev " : 'First Previous '; /* limit the number of page links displayed */ $begin = $pg - 4; while($begin < 1) $begin++; $end = $pg + 4; while($end > $pages) $end--; for($i=$begin; $i<=$end; $i++) echo ($i == $pg) ? ' ['.$i.'] ' : ' <a href="'. $_SERVER['PHP_SELF'].$query_str.$i.'">'.$i.'</a> '; /* display ending navigation */ echo ($pg < $pages) ? " $next $last" : ' Next Last'; echo "</b></font></td></tr></table></div>";}/* set pagination variables */$display = 5;$pg = (isset($_REQUEST['pg']) && ctype_digit($_REQUEST['pg'])) ? $_REQUEST['pg'] : 1;$start = $display * $pg - $display;/* paginating from a database */mysql_connect("localhost","root","");mysql_select_db("books");$result = mysql_query("SELECT count(*) FROM main WHERE ISBN='$ISBN'");$total = mysql_result($result, 0);$query = "SELECT * from main WHERE ISBN='$ISBN' ORDER BY 'ID' ASC LIMIT $start, $display";$news = mysql_query($query) or die(mysql_error());$rows = mysql_num_rows($news);if($rows >= 1) {paginate($total, $display, $pg, $total);}[/code]keep in mind i am cutting out some from my code that i believe u don't need Quote Link to comment https://forums.phpfreaks.com/topic/33231-pagination/#findComment-155312 Share on other sites More sharing options...
simcoweb Posted January 7, 2007 Share Posted January 7, 2007 Can you post ALL the code so we can also see your query? Quote Link to comment https://forums.phpfreaks.com/topic/33231-pagination/#findComment-155315 Share on other sites More sharing options...
.josh Posted January 8, 2007 Share Posted January 8, 2007 $PHP_SELF is an environment var that holds the current script's path/name. You really should be using $_SERVER['PHP_SELF'] instead of $PHP_SELF, though. However, this would not be what is causing your PREV 123 NEXT text to not become links.Are you sure that $page variable is getting passed right? somewhere before you use this $page variable, you should have something like this:[code=php:0]$page = $_GET['page'];[/code]right? Quote Link to comment https://forums.phpfreaks.com/topic/33231-pagination/#findComment-155590 Share on other sites More sharing options...
simcoweb Posted January 8, 2007 Share Posted January 8, 2007 This is the easiest pagination code i've ever worked with. First an example of how it's laid out in a script:[code]<?php// product display pagesession_start();include 'db-config.inc.php';include 'header.inc.php';include 'db_cart_functions.php';db_connect();// 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 = 20;// Figure out the limit for the query based on the current page number.$from = (($page * $max_results) - $max_results);$sql = "SELECT * FROM products ORDER BY id LIMIT $from, $max_results";$results = mysql_query($sql) or die(mysql_error());$num_rows = mysql_num_rows($results);// output our display of the productsecho "<table width='600' border='0' align='center'>";echo "<tr><td align='center'>There are $num_rows product available.\n</td></tr>";echo "<tr><td>";// Build Page Number Hyperlinksecho "<center>Select a Page<br /><br />";for($i = 1; $i <= $total_pages; $i++){ echo " | "; if(($page) == $i){ echo "Page $i "; } else { echo "<a class=\"body\" href=\"".$_SERVER['PHP_SELF']."?page=$i\">Page $i</a> "; }}echo "</center><p>\n";echo "</td></tr>";// loop through our resultswhile ($row = mysql_fetch_array($results)){ echo "<tr> <td>".$row['image']."</td><td>".$row['product_name']."<br>".$row['product_desc']."</td> <td><form action='addtocart.php?product_id=$product_id' method='POST'><input type='text' name='quantity' size='4'> <input type='submit' name='addtocart' value='Add To Cart'></form></td> </tr>";}echo "</table>\n";include 'footer.php';?>[/code]The way it's set up is to display at the top of the results, centered. And now just the pagination code so you can have it separate:[code]// 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 = 75;// Figure out the limit for the query based on the current page number.$from = (($page * $max_results) - $max_results);//SQL clauseORDER BY id LIMIT $from, $max_results// Build Page Number Hyperlinksecho "<center>Select a Page<br /><br />";for($i = 1; $i <= $total_pages; $i++){ echo " | "; if(($page) == $i){ echo "Page $i "; } else { echo "<a class=\"body\" href=\"".$_SERVER['PHP_SELF']."?page=$i\">Page $i</a> "; }}echo "</center><p>\n";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33231-pagination/#findComment-155649 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.