2016-07-31 43 views
0

進賬如何分頁數據讓說,如果有13個記錄在第三頁從DATABSE

最新的5或第二頁9-13在第一頁, 4-8和1-3我已經試過,但它的第一頁只有

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "mysqli_login"; 

// Create connection 
$connection= new mysqli($servername, $username, $password, $dbname); 
$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT 5")or die(mysqli_error($connection)); 
while ($row = mysqli_fetch_array($query)) { 
    $fileName = $row['name']; 
    $fileContents = file_get_contents("txt/$fileName"); 
    $poster = $row['submittedby']; 
    $date = $row['trn_date']; 
    echo ("posted by :$poster | posted date : $date"); 
    echo ("$fileContents"); 
} 
?> 
+0

您需要使用'LIMIT',但指定頁面和偏移量 - 像'LIMIT 2,10'〜第2頁共10條記錄 – RamRaider

回答

0

您的代碼應該是這樣的

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "test_db"; 

// Create connection 
$connection= new mysqli($servername, $username, $password, $dbname); 

$current_page = 1; // 1=>refer to the first page, 2=> second page and so on.. 
if(!empty($_GET['page_no']){ 
$current_page = $_GET['page_no']; 
} 

$to = "5"; // it is no of record which you wants to show on each page, you can change it's value as per your need. 
$from = ($current_page - 1) * $to; 


$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT $from , $to")or die(mysqli_error($connection)); 
while ($row = mysqli_fetch_array($query)) { 
    $fileName = $row['name']; 
    $fileContents = file_get_contents("txt/$fileName"); 
    $poster = $row['submittedby']; 
    $date = $row['trn_date']; 
    echo ("posted by :$poster | posted date : $date"); 
    echo ("$fileContents"); 
} 
?> 

您的網址應該是這樣http://localhost/projects/?page_no=1 替換 「http://localhost/projects/」 與您實際的URL

希望這將幫助!