错字:
$page = (isset($_REQUEST['page'])) ? 1 : $_REQUEST['page'];
变成
$page = (!isset($_REQUEST['page'])) ? 1 : $_REQUEST['page'];
这是一个非常简单的解决方案。您基本上“读取”分页代码中的 URL/查询字符串并自动“将其附加到您的 URL。我为您编写了一些简单的即插即用代码 - 这很容易做到这一点(您只需要多一行代码和一个包含文件 - 'inc_pagination.php', - 到需要分页的任何页面。不修改现有代码):
/*pagination */
$per_page = 5;
include 'inc_pagination.php'; /* FIRST LINE ADDED TO YOUR CODE*/
$pages_query = mysql_query("SELECT COUNT('file_name') FROM users WHERE `user_name` = '{$_SESSION['username']}' ") or die(mysql_error());
$pages = ceil(mysql_result($pages_query, 0) /$per_page);
/* REMOVING THESE TWO LINES */
/* $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page; */
$data = mysql_query("SELECT * FROM users LIMIT $start, $per_page") or die(mysql_error());
echo "<table border cellpadding=1>";
while($info = mysql_fetch_array( $data ))
{ ?><?php
echo "<tr>";
echo "<td>".$info['user_name'] . "</td> ";
?>
<td> <a href="download.php?user_id=<?php echo $info['user_id'];?>"> <?php echo $info['file_name']; ?></a></td>
<?php
echo "<td>".date('d/m/Y') . "</td>" ;
}
echo "</tr>";
echo "</table>";
/* 2nd LINE ADDED (Replaces Existing Page Code) - Add this wherever you need page numbers displayed */
echo paginate_me(ceil(mysql_result($pages_query, 0)/$per_page), $per_page);
请注意,您显然需要删除现有的分页代码块:
if($pages >= 1 && $page <=$pages){
for ($x = 1; $x<=$pages; $x++){
echo ($x == $page) ? '<strong><a href="?page='.$x.'">' .$x. '</a> </strong>' : '<a href="?page=' .$x. '">' .$x. ' </a> ';
}
}
最后是包含文件'inc_pagination.php'的代码。请注意,这个具有所有的花里胡哨 - 包括用于悬停/鼠标悬停和其他视觉效果的 javascript - 如果您愿意,我还可以包括一个非常简单的纯文本版本!
<?
//The number of page numbers you want displayed accross page in pagination bar
$nav_links_per_row = 35;
$page = (!isset($_REQUEST['page'])) ? 1 : $_REQUEST['page'];
$start = ($page == 1) ? 0 : ($page - 1) * $per_page ;
$per_page = (!$per_page) ? 5 : $per_page;
function paginate_me($no_of_pages, $per_page, $nav_links_per_row = 35) {
$page = 1;
$page = $_REQUEST['page'];
$x_mid_start = 1;
$x_mid_end = $no_of_pages;
/* START: Here is the main code that detects all your existing QUERY STRING and AUTOMATICALLY includes them in the Page Links */
$x_url = urldecode($_SERVER['HTTP_REFERER']);
parse_str($x_url, $output);
$qs = explode('?',urldecode($x_url));
/* END: QUERY STRING DETECT CODE */
$p_me .= "<br><table align = 'Center' width = '90%' cellpadding = '0' border = '0'><tr>";
if ($no_of_pages > 35)
{
// Go Back 5
if ( $page > 5 ){
$bg_color = "White";
$ObjTopBar = '"jTopBar_start"';
$q_str = $_SERVER['QUERY_STRING'];
$jump = (int)$page - 5;
$title = "User Files: Jump Back 5 Pages";
$this_page = str_replace("page=$page","page=$jump","?".$q_str);
$p_me .= "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: #333333; vertical-align:middle;padding-top: 2px; overflow:auto'>-</div></a></td>";
}
//1 to 10
for ($i = 1; $i <= 8; $i++)
{
if ($i == $page) $bg_color = "orange";
else $bg_color = "#333333";
if (!$page) $bg_color = "orange";
$ObjTopBar = '"jTopBar'.$i.'"';
$q_str = $_SERVER['QUERY_STRING'];
$this_page = str_replace("page=$page","page=$i","?".$q_str);
$title = "User Files: Jump to Page $i";
$p_me .= "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'>".$i."</div> </a></td>";
}
//Set Starting Page Numbers for Mid Block If Page Count is > 35
$p_me .= "<td> <font color=white>... </td>";
$x_mid_start = floor($no_of_pages / 2) - 5 ;
$x_mid_end = floor($no_of_pages / 2) + 5 ;
//if selected page is between half - 5 to half + 5 SHOW start: selected page, end: selected page + 9
if (($page > 8) && ($page < $no_of_pages - 8)) {
$x_mid_start = $page - 5;
$x_mid_end = $page + 5;
}
}
//Empty Blocks...First Few Spaces...if Page Count < 35
if ($no_of_pages < 35) {
for ($k = 1; $k <= floor((35 - $no_of_pages) / 2); $k++)
{
$ObjTopBarLinks = '"divBEnd_'.$k.'"';
$ObjTopBarEmpty = '"divBStart'.$k.'"';
$p_me .= "<td><div id=".$ObjTopBarEmpty." onMouseOver='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' onMouseOut='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' style='cursor: pointer; cursor: hand; border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: grey; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'> </div></td>";
}
}
// Print Page Numbers in the Center
for ($i = $x_mid_start; $i <= $x_mid_end; $i++)
{
if ($i == $page) $bg_color = "orange";
else $bg_color = "#333333";
if (!$page) $bg_color = "orange";
$ObjTopBar = '"jTopBar'.$i.'"';
$q_str = $_SERVER['QUERY_STRING'];
$this_page = str_replace("page=$page","page=$i","?".$q_str);
$title = "User Files: Jump to Page $i";
$p_me .= "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'>".$i."</div> </a></td>";
}
//Empty Blocks...Last Few Spaces...If Page Count < 35
if ($no_of_pages < 35) {
for ($k = 1; $k <= ceil(($nav_links_per_row - $x_mid_end)/2); $k++)
{
$ObjTopBarLinks = '"divBStart'.$k.'"';
$ObjTopBarEmpty = '"divBEnd_'.$k.'"';
$p_me .= "<td><div id=".$ObjTopBarEmpty." onMouseOver='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' onMouseOut='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' style='cursor: pointer; cursor: hand; border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: grey; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'> </div></td>";
}
}
else
{
//Page Count is > 35
$p_me .= "<td> <font color=white>... </td>";
//no_of_pages-10 to end
for ($i = $no_of_pages - 8; $i <= $no_of_pages; $i++)
{
if ($i == $page) $bg_color = "orange";
else $bg_color = "#333333";
if (!$page) $bg_color = "orange";
$ObjTopBar = '"jTopBar'.$i.'"';
$q_str = $_SERVER['QUERY_STRING'];
$this_page = str_replace("page=$page","page=$i","?".$q_str);
$title = "User Files: Jump to Page $i";
$p_me .= "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'>".$i."</div> </a></td>";
}
// Go Forward 5
if ( $page < $no_of_pages - 5 ){
$bg_color = "White";
$ObjTopBar = '"jTopBar_end"';
$q_str = $_SERVER['QUERY_STRING'];
$jump = (int)$page + 5;
$title = "User Files: Jump Forward 5 Pages";
$this_page = str_replace("page=$page","page=$jump","?".$q_str);
$p_me .= "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: #333333; vertical-align:middle;padding-top: 2px; overflow:auto'>+</div></a></td>"; }
}
$p_me .= "</tr></table><br>";
return $p_me;
}
?>
<script type="text/javascript">
function onMouseOverOut(Obj) {
setTimeout(function() { onMouseOverOut1(Obj) }, 300);
}
function onMouseOverOut1(x) {
var ODiv = document.getElementById(x);
var currBg = ODiv.style.background;
if (currBg == "orange") return false;
if (x == "jTopBar_start") return false;
if (x == "jTopBar_end") return false;
if (ODiv.style.background.indexOf("red") < 0)
ODiv.style.background = "red";
else
if (ODiv.style.background.indexOf("#333333") < 0)
ODiv.style.background = "#333333";
}
function onMouseOverOutE(Obj) {
setTimeout(function() { onMouseOverOutE1(Obj) }, 300);
}
function onMouseOverOutE1(Obj) {
var ODiv = document.getElementById(Obj);
if (ODiv.style.border.indexOf("grey") < 0)
ODiv.style.border = "1px solid grey";
else
if (ODiv.style.border.indexOf("silver") < 0)
ODiv.style.border = "1px solid silver";
if (ODiv.style.background.indexOf("white") < 0)
ODiv.style.background = "white";
else
if (ODiv.style.background.indexOf("grey") < 0)
ODiv.style.background = "grey";
}
</script>