2011-07-13 52 views
2

我在mysql中有一個表格 因爲它有很多行我想把每個10行放在一個頁面中並通過點擊一個鏈接向我展示下10行 有沒有解決方案?如何在PHP中對Mysql表格進行分頁

+0

參見教程 - http://net.tutsplus.com/tutorials/php/how-to-paginate-data -with-php/ – Sukumar

+0

謝謝Sukumar – Nickool

回答

10

這是真的真棒http://www.phpsimplicity.com/tips.php?id=1

它就是這麼簡單!無需使用大型課程!我很高興:d

<html> 

<head> 
<title>Paginate</title> 
</head> 
<body> 
<form method='get'> 
<?PHP 
$connection=Mysql_connect('server','user','pass'); 
     if(!$connection) 
     { 
      echo 'connection is invalid'; 
     } 
     else 
     { 
      Mysql_select_db('DB',$connection); 

     } 
//check if the starting row variable was passed in the URL or not 
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) { 
    //we give the value of the starting row to 0 because nothing was found in URL 
    $startrow = 0; 
//otherwise we take the value from the URL 
} else { 
    $startrow = (int)$_GET['startrow']; 
} 
//this part goes after the checking of the $_GET var 
$fetch = mysql_query("SELECT * FROM sample LIMIT $startrow, 10")or 
die(mysql_error()); 
    $num=Mysql_num_rows($fetch); 
     if($num>0) 
     { 
     echo "<table border=2>"; 
     echo "<tr><td>ID</td><td>Drug</td><td>quantity</td></tr>"; 
     for($i=0;$i<$num;$i++) 
     { 
     $row=mysql_fetch_row($fetch); 
     echo "<tr>"; 
     echo"<td>$row[0]</td>"; 
     echo"<td>$row[1]</td>"; 
     echo"<td>$row[2]</td>"; 
     echo"</tr>"; 
     }//for 
     echo"</table>"; 
     } 
//now this is the link.. 
echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a>'; 

$prev = $startrow - 10; 

//only print a "Previous" link if a "Next" was clicked 
if ($prev >= 0) 
    echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'">Previous</a>'; 
?> 
</form> 
</body> 
</html> 

通過rickyduck的方式鏈接也很好

+0

沒有它的意思是玩的方式:) –

+0

非常簡單和工作的答案 –