2014-01-25 36 views
3

我想在php中使用分頁與使用ajax和數據來自文件。在搜索關鍵詞時,頁面顯示文件中的前20條記錄,但現在我希望文件中剩餘的下一條記錄,並且應該使用分頁。任何建議嗎?從文件分頁用php和ajax

$keyword=$_POST['data']; 
$file = file_get_contents("http://localhost:8080/searchengine/searchDeals?searchKeyword=".$keyword.""); 
$output = "["; 
$line = 1; 
if (!($fp = fopen($file, "r"))) 
exit("Unable to open the input file."); 
while(!feof($fp) && $line <= 20) 
{ 
if($line != 20){ 
$output = $output.fgets($fp).","; 
    }else{  
$output = $output.fgets($fp); 
} 
$line++; 
} 
fclose($fp); 
$output = $output."]"; 
echo $output; 
+1

請隨意添加一些您嘗試過的代碼。 – Jesse

+0

我手動使用現在我想用ajax來做。我發佈了我的手冊代碼 – Mayur

+0

我已經完成了它從文件發送前20條記錄。到ajax,並在那裏desplays記錄 – Mayur

回答

1

首先,您需要計算總頁數。它是floor(number_of_lines_in_file/20)

要切換到不同的頁面,您必須通過選定的頁面變量(例如通過$ _GET)並獲取從$page_number - 1 * 20$page_number * 20的行。您可以得到循環線路或想到的東西更先進喜歡使用fseek也許......

最簡單的方法很可能是使用PHP的file()函數來代替,這將文件保存到一個數組,你只需要使用array_slice就可以得到所需的部分線。

+0

感謝你,我會嘗試。 – Mayur