分页与PHP和AJAX的文件分页、文件、PHP、AJAX

2023-09-10 19:26:53 作者:牙齿晒太阳

我想使用分页PHP中的使用Ajax和数据从文件中来了。而搜索的关键词的页面显示第一个20条记录从一个文件,但现在我想从文件中下一个剩余记录,并应使用分页。任何建议吗?

  $关键词= $ _ POST ['数据'];
$文件=的file_get_contents(HTTP://本地主机:8080 /的搜索引擎/ searchDeals searchKeyword =$关键字);
$输出=[;
$行= 1;
如果(!($计划生育=的fopen($文件,R)))
退出(无法打开输入文件。);
而(的feof($ FP)及!&安培; $线474 = 20)
{
如果($行!= 20){
 $输出= $ output.fgets($ FP),。
  }其他{
 $输出= $ output.fgets($ FP);
}
 $行++;
}
fclose函数($ FP);
]$输出= $输出。
回声$输出;
 

解决方案

首先,你需要计算的总页数。这是地板(number_of_lines_in_file / 20)

要切换到不同的页面,你就必须通过所选择的页面变量(通过$ _ GET为例),并从 $ PAGE_NUMBER线 - 1 * 20 到< $ C C> $ PAGE_NUMBER $ * 20 。你可以得到的环行或想一些更高级的如使用 fseek的也许......

但是最简单的方法很可能会使用PHP的文件()函数来代替,这将文件保存到一个数组,你'会只需要获得使用行 array_slice 所需的部分。

使用PHP和AJAX的XML编程

I want to use pagination in php with the use ajax and data are coming from file. while searching a key word the page shows 1st 20 records from a file but now i want next remaining record from file and it should use pagination. Any suggestion please?

$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;

解决方案

First you'll need to count the total number of pages. It's floor(number_of_lines_in_file / 20).

To switch to different pages you'll have to pass the selected page variable(through $_GET for example) and get lines from $page_number - 1 * 20 to $page_number * 20. You can either get the lines from the loop or think of something more advanced like using fseek maybe...

But the easiest approach would probably be using PHP's file() function instead, which will save the file into an array, and you'll only have to get required portion of lines using array_slice.