2013-08-04 47 views
0

首先我對MySQl很新,所以我的代碼不是最好的。分頁不會通過頁面

我試圖爲我的搜索結果添加分頁,但我無法弄清楚爲什麼我無法移動到下一頁。搜索結果將顯示在頁面頂部,並顯示下一個和最後一個鏈接,但點擊下一個鏈接將不會將我帶到第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

請嘗試將代碼分解到相關部分,例如發送sql查詢 –

回答

2

那麼,首先,你不應該使用mysql_功能;他們已經被棄用了一段時間了。這就是說,我不知道你在哪裏設置$pagenum,所以我認爲這是問題所在。該檢查$pagenumif()之前加入這個地方:

$pagenum = $_GET['pagenum']; 

當然,你不想做,在生產,除非你喜歡被黑客入侵你的數據庫。查看filter_vars並使用參數化的mysqli函數或PDO。

+0

這是非常感謝你。 – user2447848

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']; 
} 
+0

好的,我將代碼行更改爲您的建議,並且它仍然執行相同的操作,下一個按鈕將更改URL中的頁碼,但不會移動頁面,然後不會再繼續。如果我點擊「最後」鏈接,它將移動到URL中的最後一頁,但不在頁面本身上,因此單擊第2頁上的下一個說明,但它不是並且不會前進到第3頁並單擊最後一個說明它在第16頁上但它仍然在第1頁 – user2447848

+0

@ user2447848我更新了答案。 – hallaji

+0

@ user2447848當pagenum' name = value未在url中設置時,如果您未通過isset($ _ GET ['pagenum'])'檢查'pagenum',將會出現錯誤。 – hallaji

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>"; 
    ?>