Php 分页赢得';不要翻页
首先,我是MySQl的新手,所以我的代码不是最好的 我正在尝试将分页添加到搜索结果中,但我不明白为什么无法移动到下一页。搜索结果显示,顶部显示第1页,共3页,显示下一个和最后一个链接,但单击下一个链接不会将我带到第2页。页面号表示它在URL中更改为2,然后更改为3,但页面没有更改 以下是我正在使用的代码:Php 分页赢得';不要翻页,php,mysql,Php,Mysql,首先,我是MySQl的新手,所以我的代码不是最好的 我正在尝试将分页添加到搜索结果中,但我不明白为什么无法移动到下一页。搜索结果显示,顶部显示第1页,共3页,显示下一个和最后一个链接,但单击下一个链接不会将我带到第2页。页面号表示它在URL中更改为2,然后更改为3,但页面没有更改 以下是我正在使用的代码: <?php // Connects to your Database mysql_connect("localhost", "root", "xxxx") or die(m
<?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> ";
}
?>
服务器上可能没有“启用”全局变量
从PHP5.3.0开始,使用register globals已被弃用,并从PHP5.4.0开始删除
试着这样做:
而不是这一行:
if (!(isset($pagenum))){
$pagenum = 1;
}
使用:
首先,您不应该使用mysql\uuz
函数;他们已经被弃用一段时间了。话虽如此,我看不出您在哪里设置了$pagenum
,因此我认为这就是问题所在。在检查$pagenum
的if()
之前的某个地方添加此项:
$pagenum = $_GET['pagenum'];
当然,你不想在生产中这样做,除非你喜欢你的数据库被黑客攻击。查看filter\u vars
并使用参数化的mysqli
函数或PDO。这是代码的错误修复和优化版本
<?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>";
?>
请尝试将您的代码分解为相关部分,例如,sent sql queriesOk我将代码行更改为您建议的代码行,但它仍然执行相同的操作,“下一步”按钮将更改URL中的页码,但不会移动页面,然后它将不再继续。如果我点击“last”链接,它将移动到URL中的最后一页,但不会移动到页面本身,因此点击next say在第2页,但不会移动到第3页,点击last say在第16页,但仍在第6页1@user2447848如果未通过isset($\u get['pagenum'])检查pagenum
,则会出现错误
whenpagenum
name=url中未设置的值。
<?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>";
?>