2013-08-04 4 views
0

먼저 MySQl을 처음 사용하여 코드가 적합하지 않습니다.페이지 매기기가 페이지를 통해 이동하지 않습니다

검색 결과에 페이지 매김을 추가하려고하는데 왜 다음 페이지로 이동할 수 없는지 알 수 없습니다. 검색 결과가 표시되고 상단의 3 페이지 중 1 페이지에 다음 링크와 마지막 링크가 표시되지만 다음 링크를 클릭하면 페이지 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을 설정했는지는 모르겠다. 그래서 나는 그것이 문제라고 생각한다. $pagenum 확인하는 if() 전에이 곳을 추가 : 물론

$pagenum = $_GET['pagenum']; 

, 당신은 당신이 당신의 데이터베이스가 해킹 좋아하지 않는, 생산 그렇게하고 싶지 않아. filter_vars을보고 매개 변수화 된 mysqli 함수 또는 PDO를 사용하십시오.

+0

정말 고마워요. – user2447848

0

아마 전역 변수가 서버에서 "사용"되지 않은 : 여기

내가 사용하고 코드입니다. 대신이 라인의

:

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

사용 전역 등록 사용

은 5.4.0 Why?

이 방법을 시도 PHP 5.3.0부터 배제 및 PHP의부터 제거 :

if (!isset($_GET['pagenum'])){ 
    $pagenum = 1; 
} 
else { 
    $pagenum = $_GET['pagenum']; 
} 
+0

좋아, 당신이 제안한 코드 줄을 변경하고 여전히 같은 일을, 다음 단추는 URL에서 페이지 번호를 변경하지만 페이지를 이동하지 않습니다 그리고 그것은 더 이상 가지 않을 것입니다. "마지막"링크를 클릭하면 URL의 마지막 페이지로 이동하지만 페이지 자체에는 표시되지 않으므로 다음 페이지를 2 번 클릭하면 표시되지만 3 번 페이지로 이동하지 말고 16 페이지에 마지막으로 표시됩니다. 하지만 여전히 1 페이지에 있습니다. – user2447848

+0

@ user2447848 답변을 업데이트했습니다. – hallaji

+0

@ user2447848 url에'pagenum' name = value이 설정되어 있지 않으면'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>"; 
    ?> 
관련 문제