首先,我对MySQl很新,所以我的代码不是最好的。
我正在尝试为搜索结果添加分页,但我无法弄清楚为什么我无法移动到下一页。显示搜索结果,并在顶部显示第1页,共3页,显示下一个和最后一个链接,但点击下一个链接不会将我带到第2页。页码说它在URL中更改为2然后是3但页面不会改变。
以下是我正在使用的代码:
<?php
// Connects to your Database
mysql_connect("localhost", "root", "xxxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}
//Here we count the number of results
$data = mysql_query("SELECT * FROM players") or die(mysql_error());
$rows = mysql_num_rows($data);
//This is the number of results displayed per page
$page_rows = 10;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM players $max") or die(mysql_error());
// Define $color=1
$color="1";
echo '<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0">';
echo '<th>ID</th><th>Division</th><th>Club</th><th>Roster Number</th><th>Last Name</th><th>First Name</th><th>Registered</th><th>Payment</th></th><th>View Player</th><th>Edit Player</th><th>Check Out</th><th>Check In</th><th>Make Badge</th><th>Delete</th>';
//This is where you display your query results
while($row = mysql_fetch_array( $data_p))
// If $color==1 table row color = #FFC600
if($color==1){
echo "<tr bgcolor='#C6E7F7'>
<td><center>".$row['id']."</center></td><td><center>".$row['division']."</center></td><td><center>".$row['club']."</center></td><td><center>".$row['roster_number']."</center></td><td><center>".$row['lname']."</center></td><td><center>".$row['fname']."</center></td><td><center>".$row['registered']."</center></td><td><center>".$row['pay_status']."</center></td><td><center><a href=player_verification.php?id=$row[id]><img src=images/icons/view.png height='30' width='30' border='0'/></center></td><td><center><a href=edit_player.php?id=$row[id]><img src=images/icons/edit.png height='25' width='25' border='0'/></center></td><td><center><a href=equipment_checkout.php?id=$row[id]><img src=images/icons/out-icon.png height='30' width='30' border='0'/></center></td><td><center><a href=equipment_checkin.php?id=$row[id]><img src=images/icons/checkin.png height='30' width='30' border='0'/></center></td><td><center><a href=make_badge.php?id=$row[id]><img src=images/icons/badge.png height='30' width='30' border='0'/></center></td><td><center><a href=delete.php?id=$row[id]><img src=images/icons/delete.gif height='20' width='20' border='0'/></center></td></tr>";
// Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, use this table row color
else {
echo "<tr bgcolor='#FFFFFF'>
<td><center>".$row['id']."</center></td><td><center>".$row['division']."</center></td><td><center>".$row['club']."</center></td><td><center>".$row['roster_number']."</center></td><td><center>".$row['lname']."</center></td><td><center>".$row['fname']."</center></td><td><center>".$row['registered']."</center></td><td><center>".$row['pay_status']."</center></td><td><center><a href=player_verification.php?id=$row[id]><img src=images/icons/view.png height='30' width='30' border='0'/></center></td><td><center><a href=edit_player.php?id=$row[id]><img src=images/icons/edit.png height='25' width='25' border='0'/></center></td><td><center><a href=equipment_checkout.php?id=$row[id]><img src=images/icons/out-icon.png height='30' width='30' border='0'/></center></td><td><center><a href=equipment_checkin.php?id=$row[id]><img src=images/icons/checkin.png height='30' width='30' border='0'/></center></td><td><center><a href=make_badge.php?id=$row[id]><img src=images/icons/badge.png height='30' width='30' border='0'/></center></td><td><center><a href=delete.php?id=$row[id]><img src=images/icons/delete.gif height='20' width='20' border='0'/></center></td></tr>";
// Set $color back to 1
$color="1";
}
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
答案 0 :(得分:2)
嗯,首先,您不应该使用mysql_
函数;他们已经被弃用了一段时间了。话虽如此,我没有看到你设置$pagenum
的位置,所以我认为这就是问题所在。在检查if()
的{{1}}之前的某处添加:
$pagenum
当然,您不希望在生产中这样做,除非您喜欢数据库被黑客入侵。查看$pagenum = $_GET['pagenum'];
并使用参数化filter_vars
函数或PDO。
答案 1 :(得分:0)
也许全局变量不是服务器上的“启用”。
自PHP 5.3.0起,使用寄存器全局变量已被弃用,自PHP 5.4.0起已被删除Why?
尝试这种方式:
而不是这一行:
if (!(isset($pagenum))){
$pagenum = 1;
}
使用:
if (!isset($_GET['pagenum'])){
$pagenum = 1;
}
else {
$pagenum = $_GET['pagenum'];
}
答案 2 :(得分:0)
这是代码的修复和优化版本。
<?php
// Connects to your Database
mysql_connect("localhost", "root", "xxxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
//This is the number of results displayed per page
$page_rows = 10;
//This checks to see if there is a page number. If not, it will set it to page 1
if (!$_GET['pagenum'])
$pagenum = 1;
//Here we count the number of results
$data = mysql_query("SELECT * FROM players") or die(mysql_error());
$rows = mysql_num_rows($data);
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
$pagenum = max(min($pagenum, $last), 1);
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM players $max") or die(mysql_error());
?>
<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0">
<th>ID</th><th>Division</th><th>Club</th><th>Roster Number</th><th>Last Name</th><th>First Name</th><th>Registered</th><th>Payment</th></th><th>View Player</th><th>Edit Player</th><th>Check Out</th><th>Check In</th><th>Make Badge</th><th>Delete</th>
<?php
//This is where you display your query results
$odd = false;
while($row = mysql_fetch_array( $data_p)) {
if($odd = !$odd)
echo "<tr bgcolor='#C6E7F7'>";
else
echo "<tr bgcolor='#FFFFFF'>";
echo "<td><center>".$row['id']."</center></td><td><center>".$row['division']."</center></td><td><center>".$row['club']."</center></td><td><center>".$row['roster_number']."</center></td><td><center>".$row['lname']."</center></td><td><center>".$row['fname']."</center></td><td><center>".$row['registered']."</center></td><td><center>".$row['pay_status']."</center></td><td><center><a href=player_verification.php?id=$row[id]><img src=images/icons/view.png height='30' width='30' border='0'/></center></td><td><center><a href=edit_player.php?id=$row[id]><img src=images/icons/edit.png height='25' width='25' border='0'/></center></td><td><center><a href=equipment_checkout.php?id=$row[id]><img src=images/icons/out-icon.png height='30' width='30' border='0'/></center></td><td><center><a href=equipment_checkin.php?id=$row[id]><img src=images/icons/checkin.png height='30' width='30' border='0'/></center></td><td><center><a href=make_badge.php?id=$row[id]><img src=images/icons/badge.png height='30' width='30' border='0'/></center></td><td><center><a href=delete.php?id=$row[id]><img src=images/icons/delete.gif height='20' width='20' border='0'/></center></td></tr>";
}
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
$previous = $pagenum-1;
$next = $pagenum+1;
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum > 1)
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a>";
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum < $last)
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a>";
?>