Problems with Pagination Script
- chibuki
- Novice


- Joined: Oct 18, 2008
- Posts: 22
- Status: Offline
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:
And this is the HTML:
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.
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);
?>
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);
?>
- <?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);
- ?>
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.'">« Newer</a></div>';
}
if($next<$totalrecords)
{
echo '<div class="alignright"><a href="tsundereornot2.php?p='.$next.'">Older »</a></div>';
}
mysql_free_result($result);
?>
</div>
</div>
</div>
</body>
</html>
<?php
mysql_close($connection);
?>
<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.'">« Newer</a></div>';
}
if($next<$totalrecords)
{
echo '<div class="alignright"><a href="tsundereornot2.php?p='.$next.'">Older »</a></div>';
}
mysql_free_result($result);
?>
</div>
</div>
</div>
</body>
</html>
<?php
mysql_close($connection);
?>
- <!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.'">« Newer</a></div>';
- }
- if($next<$totalrecords)
- {
- echo '<div class="alignright"><a href="tsundereornot2.php?p='.$next.'">Older »</a></div>';
- }
- mysql_free_result($result);
- ?>
- </div>
- </div>
- </div>
- </body>
- </html>
- <?php
- mysql_close($connection);
- ?>
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


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
November 4th, 2008, 10:34 pm
- chibuki
- Novice


- Joined: Oct 18, 2008
- Posts: 22
- Status: Offline
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.
<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


- Joined: Jul 06, 2004
- Posts: 1890
- Loc: Las Vegas
- Status: Offline
- chibuki
- Novice


- Joined: Oct 18, 2008
- Posts: 22
- Status: Offline
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?
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);
?>
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);
?>
- <?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);
- ?>
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 4 posts
- Users browsing this forum: No registered users and 224 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
