2016-07-31 32 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

希望这将帮助!