Jump to content

Having Problems With Pagination Code


jamesjmann

Recommended Posts

Hey, I got this script off phpfreaks in the post "Basic Pagination Code", and got it to work except I'm experiencing this really tiny problem. The script displays only the first row in my table, and displays 127 times, which is how many rows I have total in the table.

 

This is where I have this script running at. You can go there to see what I'm talking about:

http://www.djsmiley.net/guestbook.php

 

This is the code I got and tweaked:

<?php
//your username 
$username = "your username"; 
//your password 
$password = "your password"; 
//your mySQL server 
$host = "your hostname"; 
//The name of the database your table is in 
$database = "your database"; 

//connect, but if there is a problem, display an error message telling why there is a problem 
$conn = mysql_connect($host,$username,$password) or die("Error connecting to Database!<br>" . mysql_error()); 
//Choose the database from your mySQL server, but if there is a problem, display an error telling why 
$db = mysql_select_db($database) or die("Cannot select database!<br>" . mysql_error()); 

// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM comments";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$sql = "SELECT * FROM comments ORDER BY commentid DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
   // echo data
   if ($name == 'DJ Smiley' && $adminkey == 'adminkey') {

echo("
<div style=\"border-radius: 10px; border: solid 2px #E5E5E5; padding: 10px; width: 95%; background: #F6F6F6;\">
<p>
<a name=\"$commentid\" id=\"$commentid\"></a><img src=\"http://www.djsmiley.net/images/Icons/Arrows/Right 2.png\" class=\"fltlft2\" height=\"25\" width=\"25\"><strong>$name</strong> (<a href=\"http://whois.domaintools.com/$ip\"><font size=1>$ip</font></a>) says:
<br><p class=\"BottomBorder\"></p>
<table><tr><td><img src=\"http://www.djsmiley.net/images/Self/Self (2).jpg\" height=\"50\" width=\"50\"></td><td>$comment</td></tr></table>
<br><p class=\"BottomBorder\"></p>
added on $time<p><em>$email</em></p>
</p>
</div><br>
"); 
} elseif ($name == false) {

echo("
<div style=\"border-radius: 10px; border: solid 2px #E5E5E5; padding: 10px; width: 95%; background: #F6F6F6;\">
<p>
<a name=\"$commentid\" id=\"$commentid\"></a><img src=\"http://www.djsmiley.net/images/Icons/Arrows/Right 2.png\" class=\"fltlft2\" height=\"25\" width=\"25\"><strong>Anonymous</strong> (<a href=\"http://whois.domaintools.com/$ip\"><font size=1>$ip</font></a>) says:
<br><p class=\"BottomBorder\"></p>
<table><tr><td><img src=\"http://www.djsmiley.net/images/Icons/People/Anonymous 2.png\" height=\"50\" width=\"50\"></td><td>$comment</td></tr></table>
<br><p class=\"BottomBorder\"></p>added on $time<p><em>$email</em></p>
</p>
</div><br>
"); 

} else {
echo("
<div style=\"border-radius: 10px; border: solid 2px #E5E5E5; padding: 10px; width: 95%; background: #F6F6F6;\">
<p>
<a name=\"$commentid\" id=\"$commentid\"></a><img src=\"http://www.djsmiley.net/images/Icons/Arrows/Right 2.png\" class=\"fltlft2\" height=\"25\" width=\"25\"><strong>$name</strong> (<a href=\"http://whois.domaintools.com/$ip\"><font size=1>$ip</font></a>) says:
<br><p class=\"BottomBorder\"></p>
<table><tr><td><img src=\"http://www.djsmiley.net/images/Icons/People/Anonymous 2.png\" height=\"50\" width=\"50\"></td><td>$comment</td></tr></table>
<br><p class=\"BottomBorder\"></p>
added on $time<p><em>$email</em></p>
</p>
</div><br>
"); 
}
} // end while

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for
                 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.