webguync Posted June 16, 2007 Share Posted June 16, 2007 From the tutorial on pagination from this site I have the following code, but don't get links only text. What do I have wrong? <?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); ?> this produced PREV25 1 NEXT25 without links Link to comment https://forums.phpfreaks.com/topic/55799-pagination-links-not-working/ Share on other sites More sharing options...
PC Nerd Posted June 16, 2007 Share Posted June 16, 2007 um sounds stupid. but are you getting $page from $_GET['page'] or not? gdlk Link to comment https://forums.phpfreaks.com/topic/55799-pagination-links-not-working/#findComment-275629 Share on other sites More sharing options...
webguync Posted June 16, 2007 Author Share Posted June 16, 2007 there is currently no $_GET['page'] nor was that in the tutorial. I guess you are saying I need to add that? I pretty much took the code from the tutorial without fully understanding all of it Link to comment https://forums.phpfreaks.com/topic/55799-pagination-links-not-working/#findComment-275811 Share on other sites More sharing options...
aniesh82 Posted June 16, 2007 Share Posted June 16, 2007 hello Please add this line <?php $page = $_GET['page'] ; /* add this line */ if($page != 1){ Regards Aniesh Joseph [email protected] Link to comment https://forums.phpfreaks.com/topic/55799-pagination-links-not-working/#findComment-275831 Share on other sites More sharing options...
MrSheen Posted June 16, 2007 Share Posted June 16, 2007 You might want to add this: if(isset($_GET['page'])) { $page = $_GET['page']; } else { $page = "1"; } This way, if there is not a page, it will be able to default the page to page 1. Link to comment https://forums.phpfreaks.com/topic/55799-pagination-links-not-working/#findComment-275848 Share on other sites More sharing options...
redarrow Posted June 16, 2007 Share Posted June 16, 2007 Yhere you go a little test for your database ok. <?php //database connection. if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } $max_results = 1; $from = (($page * $max_results) - $max_results); //database query. $query="select * from what_ever DESC LIMIT $from, $max_results "; $result=mysql_query($query); while($rec=mysql_fetch_assoc($result)){ $point_query="select * from blogs where what_ever='what_evrer' "; $point_result=mysql_query($point_query)or die(mysql_error()); while($p=mysql_fetch_assoc($point_result)){ // show the information } $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM what_ever"),0); if($page > 1){ $prev = ($page - 1); echo "<div align='center'><a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">Prev</a>"; } for($i = 1; $i <= $total_pages; $i++){ if(($page) == $i){ echo "<font color='yellow'>$i</font> "; } else { echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\"></a> "; } } if($page < $total_pages){ $next = ($page + 1); // show other infomation. } } ?> Link to comment https://forums.phpfreaks.com/topic/55799-pagination-links-not-working/#findComment-275859 Share on other sites More sharing options...
webguync Posted June 16, 2007 Author Share Posted June 16, 2007 the limiting of records is working, but I am still not getting the Next and Previous records to work. The end result I would like is to display 25 records on a page, and then have a link, to the next 25, and backo the previous page (if there is one). Here is my current code <?php //start a session session_start(); //check for validity of user $db_name="people"; $table_name ="Listing"; $connection = @mysql_connect("localhost", "username", "password") or die (mysql_error()); $db = @mysql_select_db($db_name, $connection) or die (mysql_error()); $limit = 25; // Sets how many results shown per page $query_count = "SELECT count(*) FROM $table_name"; // Sets what we want to pull from the database $result_count = mysql_query($query_count); // Pulls what we want from the database $totalrows = mysql_num_rows($result_count); // This counts the number of users if(empty($page)){ // Checks if the $page variable is empty (not set) $page = 1; // If it is empty, we're on page 1 } $limitvalue = $page * $limit - ($limit); // Ex: (2 * 25) - 25 = 25 <- data starts at 25 //build and issue query $sql ="SELECT id, f_name, l_name, company FROM $table_name ORDER BY company LIMIT $limitvalue, $limit"; $result = @mysql_query($sql, $connection) or die(mysql_error()); $limit = 25; $query_count = "SELECT count(*) FROM $table_name"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ $page = 1; } $limitvalue = $page * $limit - ($limit); if(mysql_num_rows($result) == 0){ echo("Nothing to Display!"); } //create list block of results $contact_list="<ul class='vertical_list'>"; while ($row = mysql_fetch_array($result)) { $id = $row['0']; $f_name=$row['1']; $l_name=$row['2']; $company=$row['3']; $contact_list .="<li> <a href=\"show_contact.php?id=$id\">$company,$l_name, $f_name, </a>"; } $contact_list .="</li></ul>"; ?> <html> <head> <title>MySQL Results Display</title> </head> <body> <p>Select a contact from the list below, to view a contact's record</p> <? echo "$contact_list"; ?> <?php if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } $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); ?> <p><a href="Recruiters.php"> Return to main menu</a></p> </body> </html> Link to comment https://forums.phpfreaks.com/topic/55799-pagination-links-not-working/#findComment-275868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.