0

首先,我对 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> ";

} 

?>
4

3 回答 3

2

好吧,首先,您不应该使用这些mysql_功能;它们已经被弃用了一段时间。话虽这么说,我看不出你在哪里设置$pagenum,所以我认为这就是问题所在。if()在那个检查之前添加这个$pagenum

$pagenum = $_GET['pagenum'];

当然,你不想在生产环境中这样做,除非你喜欢你的数据库被黑客入侵。查看filter_vars并使用参数化mysqli函数或 PDO。

于 2013-08-04T09:01:17.823 回答
0

也许全局变量在服务器上没有“启用”。

自 PHP 5.3.0 起,使用寄存器全局变量已被弃用,自 PHP 5.4.0 起已移除。为什么?

试试这个方法:

而不是这一行:

if (!(isset($pagenum))){
    $pagenum = 1;
}

利用:

if (!isset($_GET['pagenum'])){
    $pagenum = 1;
}
else {
    $pagenum = $_GET['pagenum'];
}
于 2013-08-04T08:54:49.250 回答
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> &nbsp; <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> &nbsp; <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a>";
    ?>
于 2013-08-04T09:16:47.133 回答