Problems with Pagination Script

  • chibuki
  • Novice
  • Novice
  • chibuki
  • Posts: 22

Post 3+ Months Ago

Hi,
I need help with my next and previous links in PHP loop.

Currently I have more than 50 entries in my table (ORDER BY ID DESC). The Next and Previous links are there (in my case I use "Older" and "Newer"), but the "Older" link stopped showing after a few pages - matter of fact, in any other page ("p="). I've tried it with 25 entries per page, 10 per page, 4 per page, etc. and the "Older" link always stopped halfway. I thought something's wrong with the code or perhaps other factors.

This is the next/prev code before <html> tag:
Code: [ Select ]
<?php
include('connect.php');
$query = "SELECT * FROM TsundereOrNot ORDER BY ID DESC";
 
//Run query
$result = mysql_query($query);
if(!$result)
{
    die("Could not query the database:<br />". mysql_error());
}
 
//Format date
function formatdate( $s )
{
return date( "M j, Y", strtotime( $s ) );
}
 
$place = (isset($_GET['p'])) ? $_GET['p'] : 0;
$pagesize = 15;
$next = $place+$pagesize;
$previous = $place-$pagesize;
 
if(isset($_COOKIE['totalrecords'])){
    $totalrecords = $_COOKIE['totalrecords'];
} else {
    $query="SELECT COUNT(ID) AS Number FROM TsundereOrNot";
    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);
    $totalrecords = $row['Number'];
    setcookie("totalrecords",$totalrecords);
}
$query = "SELECT ID, Shoujo, Rank, Average, TotalVotes, FivePoints, FourPoints, ThreePoints, TwoPoints, OnePoints, DateUpdated FROM TsundereOrNot ORDER BY ID DESC LIMIT $place,$pagesize;";
$result = mysql_query($query);
?>
  1. <?php
  2. include('connect.php');
  3. $query = "SELECT * FROM TsundereOrNot ORDER BY ID DESC";
  4.  
  5. //Run query
  6. $result = mysql_query($query);
  7. if(!$result)
  8. {
  9.     die("Could not query the database:<br />". mysql_error());
  10. }
  11.  
  12. //Format date
  13. function formatdate( $s )
  14. {
  15. return date( "M j, Y", strtotime( $s ) );
  16. }
  17.  
  18. $place = (isset($_GET['p'])) ? $_GET['p'] : 0;
  19. $pagesize = 15;
  20. $next = $place+$pagesize;
  21. $previous = $place-$pagesize;
  22.  
  23. if(isset($_COOKIE['totalrecords'])){
  24.     $totalrecords = $_COOKIE['totalrecords'];
  25. } else {
  26.     $query="SELECT COUNT(ID) AS Number FROM TsundereOrNot";
  27.     $result = mysql_query($query);
  28.     $row = mysql_fetch_assoc($result);
  29.     $totalrecords = $row['Number'];
  30.     setcookie("totalrecords",$totalrecords);
  31. }
  32. $query = "SELECT ID, Shoujo, Rank, Average, TotalVotes, FivePoints, FourPoints, ThreePoints, TwoPoints, OnePoints, DateUpdated FROM TsundereOrNot ORDER BY ID DESC LIMIT $place,$pagesize;";
  33. $result = mysql_query($query);
  34. ?>


And this is the HTML:

Code: [ Select ]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MAL Tsundere Club</title>
<link href="ton.css" rel="stylesheet" type="text/css" />
</head>
 
<body>
<div id="container">
  <div id="contents">
    <table cellspacing="1" cellpadding="2" width="650">
      <tr>
        <th rowspan="2" bgcolor="#e1e5eb" width="30">#</th>
        <th rowspan="2" bgcolor="#e1e5eb" width="200">Character</th>
        <th rowspan="2" bgcolor="#e1e5eb" width="55">Rank</th>
        <th rowspan="2" bgcolor="#e1e5eb" width="62">Average</th>
        <th colspan="6" bgcolor="#e1e5eb">Votes (pts)</th>
        <th rowspan="2" bgcolor="#e1e5eb" width="105">Last Updated</th>
      </tr>
      <tr>
        <th bgcolor="#e1e5eb" width="45">Total</th>
        <th bgcolor="#e1e5eb" width="28">5</th>
        <th bgcolor="#e1e5eb" width="28">4</th>
        <th bgcolor="#e1e5eb" width="28">3</th>
        <th bgcolor="#e1e5eb" width="28">2</th>
        <th bgcolor="#e1e5eb" width="28">1</th>
      </tr>
      <?php
      while($row = mysql_fetch_assoc($result))
      {
      ?>
      <tr>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["ID"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["Shoujo"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["Rank"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["Average"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["TotalVotes"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["FivePoints"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["FourPoints"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["ThreePoints"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["TwoPoints"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["OnePoints"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo formatdate($row["DateUpdated"]);?></td>
      </tr>
      <?php } ?>
    </table>
    <div class="nextprev">
    <?php
    if($previous>=0)
    {
        echo '<div class="alignleft"><a href="tsundereornot2.php?p='.$previous.'">&laquo; Newer</a></div>';
    }
    if($next<$totalrecords)
    {
        echo '<div class="alignright"><a href="tsundereornot2.php?p='.$next.'">Older &raquo;</a></div>';
    }
    mysql_free_result($result);
    ?>
    </div>
  </div>
</div>
</body>
</html>
<?php
mysql_close($connection);
?>
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>MAL Tsundere Club</title>
  6. <link href="ton.css" rel="stylesheet" type="text/css" />
  7. </head>
  8.  
  9. <body>
  10. <div id="container">
  11.   <div id="contents">
  12.     <table cellspacing="1" cellpadding="2" width="650">
  13.       <tr>
  14.         <th rowspan="2" bgcolor="#e1e5eb" width="30">#</th>
  15.         <th rowspan="2" bgcolor="#e1e5eb" width="200">Character</th>
  16.         <th rowspan="2" bgcolor="#e1e5eb" width="55">Rank</th>
  17.         <th rowspan="2" bgcolor="#e1e5eb" width="62">Average</th>
  18.         <th colspan="6" bgcolor="#e1e5eb">Votes (pts)</th>
  19.         <th rowspan="2" bgcolor="#e1e5eb" width="105">Last Updated</th>
  20.       </tr>
  21.       <tr>
  22.         <th bgcolor="#e1e5eb" width="45">Total</th>
  23.         <th bgcolor="#e1e5eb" width="28">5</th>
  24.         <th bgcolor="#e1e5eb" width="28">4</th>
  25.         <th bgcolor="#e1e5eb" width="28">3</th>
  26.         <th bgcolor="#e1e5eb" width="28">2</th>
  27.         <th bgcolor="#e1e5eb" width="28">1</th>
  28.       </tr>
  29.       <?php
  30.       while($row = mysql_fetch_assoc($result))
  31.       {
  32.       ?>
  33.       <tr>
  34.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["ID"];?></td>
  35.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["Shoujo"];?></td>
  36.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["Rank"];?></td>
  37.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["Average"];?></td>
  38.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["TotalVotes"];?></td>
  39.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["FivePoints"];?></td>
  40.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["FourPoints"];?></td>
  41.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["ThreePoints"];?></td>
  42.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["TwoPoints"];?></td>
  43.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["OnePoints"];?></td>
  44.         <td align="center" bgcolor="#f7f8fa"><?php echo formatdate($row["DateUpdated"]);?></td>
  45.       </tr>
  46.       <?php } ?>
  47.     </table>
  48.     <div class="nextprev">
  49.     <?php
  50.     if($previous>=0)
  51.     {
  52.         echo '<div class="alignleft"><a href="tsundereornot2.php?p='.$previous.'">&laquo; Newer</a></div>';
  53.     }
  54.     if($next<$totalrecords)
  55.     {
  56.         echo '<div class="alignright"><a href="tsundereornot2.php?p='.$next.'">Older &raquo;</a></div>';
  57.     }
  58.     mysql_free_result($result);
  59.     ?>
  60.     </div>
  61.   </div>
  62. </div>
  63. </body>
  64. </html>
  65. <?php
  66. mysql_close($connection);
  67. ?>


This is the page live: http://chibuki.x10hosting.com/anime/tsundereornot2.php

It's not a complicated code. The weird thing is... I use the same exact script here and it works. :?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • chibuki
  • Novice
  • Novice
  • chibuki
  • Posts: 22

Post 3+ Months Ago

Wait a minute. I'm currently accessing it from an iMac and the script works fine. I wonder if it's my computer back home.

<edit> Tested in Opera, 9.5 and IE 6-7 and it seems to work fine.
I guess it's only defective for Firefox 3 in Windows XP although it doesn't make sense.
  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1893
  • Loc: Las Vegas

Post 3+ Months Ago

Works fine for me in FF3. I'm using Vista, though I doubt that has anything to do with it.
  • chibuki
  • Novice
  • Novice
  • chibuki
  • Posts: 22

Post 3+ Months Ago

I asked two people to test it earlier. They both have Windows XP and Firefox 3 as their main browsers and said the link doesn't go all the way to the first page. I thought it was only me but I'm not alone it seems.

I pretty much given up with my script and used the tutorial from the other thread: http://www.php-mysql-tutorial.com/php-mysql-paging.php

I'm trying to apply it to my page (live here), so far the links are working (with the # of pages called) but the loop echo is unsuccessful. Can you point on which part I need to modify?

Code: [ Select ]
<?php
include('connect.php');
 
//Format date
function formatdate( $s )
{
return date( "M j, Y", strtotime( $s ) );
}
 
//**** Pagination script ****//
// how many rows to show per page
$rowsPerPage = 20;
 
// by default we show first page
$pageNum = 1;
 
// if $_GET['page'] defined, use it as page number
if(isset($_GET['p']))
{
    $pageNum = $_GET['p'];
}
 
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
 
$query  = "SELECT ID, Shoujo, Rank, Average, TotalVotes, FivePoints, FourPoints, ThreePoints, TwoPoints, OnePoints, DateUpdated FROM TsundereOrNot ORDER BY ID DESC LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');
 
// how many rows we have in database
$query   = "SELECT COUNT(ID) AS Number FROM TsundereOrNot";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_assoc($result);
$numrows = $row['Number'];
 
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
 
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test #3</title>
<link href="ton.css" rel="stylesheet" type="text/css" />
</head>
 
<body>
<div id="container">
  <div id="contents">
    <table cellspacing="1" cellpadding="2" width="650">
      <tr>
        <th rowspan="2" bgcolor="#e1e5eb" width="30">#</th>
        <th rowspan="2" bgcolor="#e1e5eb" width="200">Character</th>
        <th rowspan="2" bgcolor="#e1e5eb" width="55">Rank</th>
        <th rowspan="2" bgcolor="#e1e5eb" width="62">Average</th>
        <th colspan="6" bgcolor="#e1e5eb">Votes (pts)</th>
        <th rowspan="2" bgcolor="#e1e5eb" width="105">Last Updated</th>
      </tr>
      <tr>
        <th bgcolor="#e1e5eb" width="45">Total</th>
        <th bgcolor="#e1e5eb" width="28">5</th>
        <th bgcolor="#e1e5eb" width="28">4</th>
        <th bgcolor="#e1e5eb" width="28">3</th>
        <th bgcolor="#e1e5eb" width="28">2</th>
        <th bgcolor="#e1e5eb" width="28">1</th>
      </tr>
      <?php
      while(list($row) = mysql_fetch_assoc($result))
      {
      ?>
      <tr>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["ID"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["Shoujo"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["Rank"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["Average"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["TotalVotes"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["FivePoints"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["FourPoints"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["ThreePoints"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["TwoPoints"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo $row["OnePoints"];?></td>
        <td align="center" bgcolor="#f7f8fa"><?php echo formatdate($row["DateUpdated"]);?></td>
      </tr>
      <?php } ?>
    </table>
    <div class="nextprev">
    <?php
    //**** Creating Previous, Next, First Page, and Last Page links ****//
    // Print 'previous' link only if we're not on page one
    if ($pageNum > 1)
    {
        $page = $pageNum - 1;
        $prev = " <a href=\"$self?p=$page\">[Prev]</a> ";
        $first = " <a href=\"$self?p=1\">[First Page]</a> ";
    }
    else
    {
        $prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
        $first = ' [First Page] '; // nor 'first page' link
    }
   
    // Print 'next' link only if we're not on the last page
    if ($pageNum < $maxPage)
    {
        $page = $pageNum + 1;
        $next = " <a href=\"$self?p=$page\">[Next]</a> ";
        $last = " <a href=\"$self?p=$maxPage\">[Last Page]</a> ";
    }
    else
    {
        $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
        $last = ' [Last Page] '; // nor 'last page' link
    }
   
    // Print the navigation link
    echo $first . $prev . " Showing page $pageNum of $maxPage pages " . $next . $last;
    ?>
    </div>
  </div>
</div>
</body>
</html>
<?php
mysql_close($connection);
?>
  1. <?php
  2. include('connect.php');
  3.  
  4. //Format date
  5. function formatdate( $s )
  6. {
  7. return date( "M j, Y", strtotime( $s ) );
  8. }
  9.  
  10. //**** Pagination script ****//
  11. // how many rows to show per page
  12. $rowsPerPage = 20;
  13.  
  14. // by default we show first page
  15. $pageNum = 1;
  16.  
  17. // if $_GET['page'] defined, use it as page number
  18. if(isset($_GET['p']))
  19. {
  20.     $pageNum = $_GET['p'];
  21. }
  22.  
  23. // counting the offset
  24. $offset = ($pageNum - 1) * $rowsPerPage;
  25.  
  26. $query  = "SELECT ID, Shoujo, Rank, Average, TotalVotes, FivePoints, FourPoints, ThreePoints, TwoPoints, OnePoints, DateUpdated FROM TsundereOrNot ORDER BY ID DESC LIMIT $offset, $rowsPerPage";
  27. $result = mysql_query($query) or die('Error, query failed');
  28.  
  29. // how many rows we have in database
  30. $query   = "SELECT COUNT(ID) AS Number FROM TsundereOrNot";
  31. $result  = mysql_query($query) or die('Error, query failed');
  32. $row     = mysql_fetch_assoc($result);
  33. $numrows = $row['Number'];
  34.  
  35. // how many pages we have when using paging?
  36. $maxPage = ceil($numrows/$rowsPerPage);
  37.  
  38. // print the link to access each page
  39. $self = $_SERVER['PHP_SELF'];
  40. ?>
  41. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  42. <html xmlns="http://www.w3.org/1999/xhtml">
  43. <head>
  44. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  45. <title>Test #3</title>
  46. <link href="ton.css" rel="stylesheet" type="text/css" />
  47. </head>
  48.  
  49. <body>
  50. <div id="container">
  51.   <div id="contents">
  52.     <table cellspacing="1" cellpadding="2" width="650">
  53.       <tr>
  54.         <th rowspan="2" bgcolor="#e1e5eb" width="30">#</th>
  55.         <th rowspan="2" bgcolor="#e1e5eb" width="200">Character</th>
  56.         <th rowspan="2" bgcolor="#e1e5eb" width="55">Rank</th>
  57.         <th rowspan="2" bgcolor="#e1e5eb" width="62">Average</th>
  58.         <th colspan="6" bgcolor="#e1e5eb">Votes (pts)</th>
  59.         <th rowspan="2" bgcolor="#e1e5eb" width="105">Last Updated</th>
  60.       </tr>
  61.       <tr>
  62.         <th bgcolor="#e1e5eb" width="45">Total</th>
  63.         <th bgcolor="#e1e5eb" width="28">5</th>
  64.         <th bgcolor="#e1e5eb" width="28">4</th>
  65.         <th bgcolor="#e1e5eb" width="28">3</th>
  66.         <th bgcolor="#e1e5eb" width="28">2</th>
  67.         <th bgcolor="#e1e5eb" width="28">1</th>
  68.       </tr>
  69.       <?php
  70.       while(list($row) = mysql_fetch_assoc($result))
  71.       {
  72.       ?>
  73.       <tr>
  74.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["ID"];?></td>
  75.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["Shoujo"];?></td>
  76.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["Rank"];?></td>
  77.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["Average"];?></td>
  78.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["TotalVotes"];?></td>
  79.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["FivePoints"];?></td>
  80.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["FourPoints"];?></td>
  81.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["ThreePoints"];?></td>
  82.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["TwoPoints"];?></td>
  83.         <td align="center" bgcolor="#f7f8fa"><?php echo $row["OnePoints"];?></td>
  84.         <td align="center" bgcolor="#f7f8fa"><?php echo formatdate($row["DateUpdated"]);?></td>
  85.       </tr>
  86.       <?php } ?>
  87.     </table>
  88.     <div class="nextprev">
  89.     <?php
  90.     //**** Creating Previous, Next, First Page, and Last Page links ****//
  91.     // Print 'previous' link only if we're not on page one
  92.     if ($pageNum > 1)
  93.     {
  94.         $page = $pageNum - 1;
  95.         $prev = " <a href=\"$self?p=$page\">[Prev]</a> ";
  96.         $first = " <a href=\"$self?p=1\">[First Page]</a> ";
  97.     }
  98.     else
  99.     {
  100.         $prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
  101.         $first = ' [First Page] '; // nor 'first page' link
  102.     }
  103.    
  104.     // Print 'next' link only if we're not on the last page
  105.     if ($pageNum < $maxPage)
  106.     {
  107.         $page = $pageNum + 1;
  108.         $next = " <a href=\"$self?p=$page\">[Next]</a> ";
  109.         $last = " <a href=\"$self?p=$maxPage\">[Last Page]</a> ";
  110.     }
  111.     else
  112.     {
  113.         $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
  114.         $last = ' [Last Page] '; // nor 'last page' link
  115.     }
  116.    
  117.     // Print the navigation link
  118.     echo $first . $prev . " Showing page $pageNum of $maxPage pages " . $next . $last;
  119.     ?>
  120.     </div>
  121.   </div>
  122. </div>
  123. </body>
  124. </html>
  125. <?php
  126. mysql_close($connection);
  127. ?>

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 46 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.