2013-03-18 52 views
1

我想使用mysql_fetch_array從數據庫顯示記錄。但問題是我不知道如何修復顯示在頁面中的大小,並將其餘記錄移動到下一頁,因爲顯示的記錄太多。如何修復mysql_fetch_array()的大小並將其顯示在多個頁面中?

例如:顯示page1中的前10行(或記錄),然後顯示另一頁中的另外10行?

繼承人是我的代碼:

$result=mysql_query("$selectKL UNION $selectKlang UNION $selectPJ UNION $selectSJ 
ORDER BY restaurant_name LIMIT 6") 

$searchQuery = search($budgetRange,$city,$category); 

while($row = mysql_fetch_array($searchQuery)) 
{ 
    echo"<tr>"; 
    echo'<td>'.$row['restaurant_id'].'</b></td>'; 
    echo'<td>'.$row['restaurant_name'].'</b></td>'; 
    echo'<td>'.$row['category'].'</b></td>'; 
    echo'<td>'.$row['budget'].'</b></td>'; 
    echo'<td>'.$row['halal'].'</b></td>'; 
    echo "</tr>"; 
} 

會有30+結果從我的數據庫顯示,但我如何在第一頁顯示前10個結果,那麼再過10結果翻頁等上?我已經在查詢中將LIMIT設置爲10,而且它只顯示10個結果,但我不知道如何存儲或傳遞結果的其餘部分(可能嗎?)。任何幫助,謝謝...請嗎?

+0

http://net.tutsplus.com/tutorials/php/how-to-paginate-data-with-php/ – Hackerman 2013-03-18 18:26:55

回答

1

分頁可能會很棘手。您需要設置一些參數(可能是查詢字符串參數)以指示足夠的信息以知道分頁的位置,即每個頁面的結果數量和頁碼,或顯示的最後結果的數量。

在您的查詢中,您可以使用LIMIT x,y其中x是起始行號,y是要檢索的行數。

分頁問題已經解決了很多次 - 這並不意味着它很容易,但這意味着有很多可用的幫助。嘗試快速搜索:https://encrypted.google.com/search?hl=en&q=php+mysql+pagination

0
/* 
    Place code to connect to your DB here. 
*/ 


$tbl_name="xyz";  //your table name 
// How many adjacent pages should be shown on each side? 
$adjacents = 3; 

/* 
    First get total number of rows in data table. 
    If you have a WHERE clause in your query, make sure you mirror it here. 
*/ 
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE approved`=1 "; 
$total_pages = mysql_fetch_array(mysql_query($query)); 
$total_pages = $total_pages['num']; 

/* Setup vars for query. */ 
$targetpage = "index.php"; //your file name (the name of this file) 
$limit = 5;         //how many items to show per page 
$page = (!empty($_GET['page']) ? $_GET['page'] : null); 

if($page) 
    $start = ($page - 1) * $limit;   //first item to display on this page 
else 
    $start = 0;        //if no page var is given, set start to 0 

/* Get data. */ 
$sql = "SELECT * FROM $tbl_name WHERE `approved`=1 ORDER BY `id` DESC LIMIT $start, $limit "; 
$result = mysql_query($sql); 

/* Setup page vars for display. */ 
if ($page == 0) $page = 1;     //if no page var is given, default to 1. 
$prev = $page - 1;       //previous page is page - 1 
$next = $page + 1;       //next page is page + 1 
$lastpage = ceil($total_pages/$limit);  //lastpage is = total pages/items per page, rounded up. 
$lpm1 = $lastpage - 1;      //last page minus 1 

include ('includes/pagination.php'); 


    while($row = mysql_fetch_array($result)) 
    { 

    //designate variables 




echo 'Your content'; 
} 
$pagination 
相關問題