Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery URL分页_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript Jquery URL分页

Javascript Jquery URL分页,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我使用jQuery和AJAX从远程存储在服务器上的PHP文件中获取一些数据。我能够获取第一个页面并在HTML表中显示结果。我现在面临另一个问题:我正在尝试获取所有页面,然后对所有这些页面进行分页 以下是我目前掌握的代码: <html> <head> <title>Food Hygiene Ratings</title> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.co

我使用jQuery和AJAX从远程存储在服务器上的PHP文件中获取一些数据。我能够获取第一个页面并在HTML表中显示结果。我现在面临另一个问题:我正在尝试获取所有页面,然后对所有这些页面进行分页

以下是我目前掌握的代码:

 <html>
 <head>
 <title>Food Hygiene Ratings</title>
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap  /3.3.5/css/bootstrap.min.css">

食物卫生评级


标题{
背景色:黑色;
颜色:白色;
文本对齐:居中;
填充:5px;}
页脚{
背景色:黑色;
颜色:白色;
明确:两者皆有;
外对齐:中心对齐;
填充:5px;}
th,td{
文本对齐:居中;
填充:8px;}
tr:n个孩子(偶数){背景色:#e65c00}
th{
背景色:#000000;
颜色:白色;}
$.getJSON(“http://xxx.ac.uk/xxxx/xxxx/xxx/hygiene/hygiene.php?op=retrieve&page=1,函数(数据){$。每个(数据,函数(索引,el){
$(“#id”).append(“+el.business+”“+el.address+”“+el.rating+”“+el.date+”;}”);
食物卫生评级
此页面将显示位于伦敦及其周边地区的快餐店和餐馆的结果


因为您使用的是ajax,所以只能加载请求的页面,而不是一次加载所有页面

您可以使用hygiene.php?op=pages检索页数

然后你知道你要显示多少页,你可以显示你的页面链接并加载第一页

要加载页面:hygiene.php?op=retrieve&page=1


或者第2页,例如hygiene.php?op=retrieve&page=2

您也可以向我们展示您的php代码吗?您是否尝试显示一个开始的分页?我添加了PHP脚本,它是服务器端的,所以我从URL获取它。我的代码只获取第一页的结果。我需要一个函数来获取所有页面,然后为它们创建分页。你尝试过什么吗?是的,我尝试过。而不是使用:$.getJSON(“.I将其替换为:$.getJSON(”。但它似乎无法获取所有页面。所以你是说我不能使用op=pages一次请求所有页面,我必须逐个请求所有页面,然后为它们创建分页?我发现有40页结果。我如何请求所有页面并为它们创建分页,这意味着我将有40个1-40之间的按钮。wh如果用户只想看到第一个页面,那么加载所有这些页面的目的是什么?这是ajax的目的:缩小请求并只加载所需的数据。想象一下,如果您有500个页面,并且加载了所有信息。我要说的是,没有任何东西允许您在php代码中一次性获取所有数据,除非您对其进行编辑。因此解决方案是首先构造分页按钮,然后显示请求的页面。只需调用hygiene.php?op=pages来获取需要显示的页面数,然后就可以按ant的方式显示分页按钮,而不必显示全部40个!如果真的想获得所有数据,就必须编辑php文件,因为它暂时不允许这样的操作。
<style>

header {
background-color: black;
color: white;
text-align: center;
padding: 5px;} 

footer {
background-color: black;
color: white;
clear: both;
ext-align: center;
padding: 5px; }

th, td {
text-align: center;
padding: 8px; }

tr:nth-child(even){background-color: #e65c00}

th {
background-color: #000000;
color: white; }} 

</style>


<script> 

$.getJSON("http://xxx.ac.uk/xxxx/xxxx/xxx/hygiene/hygiene.php?op=retrieve&page=1", function(data){$.each(data, function(index,el){
$("#id").append("<tr><td>"+el.business+"</td><td>"+el.address+"</td><td>"+el.rating+"</td><td>"+el.date+"</td></tr>"); })}); 


</script>



</head>


<header><h1>Food Hygiene Ratings</h1></header>

<p><center>This page will display the results of the fast food shops and restaurants located in and around London</center></p>


<center><table width="1032" height="41" id="id"  style=width:95%">
<tr>
<th width="130">Business</th>
<th width="130">Address</th>
<th width="100">Rating</th>
<th width="100">Date</th>
</tr>
</table></center>


</body>
</html>
<?php
 // Load the XML file from the same location as this script
 if(file_exists('city.xml')) {
 $xml = simplexml_load_file("city.xml");
 } else {
 exit('Failed to open city.xml');
 }

 // Copy the values of the relevant elements of the XML into a PHP array.
 // We're not copying everything, and we're changing the names
 // Cast to (string) is so we don't copy the SimpleXMLElement objects
 $i = 0 ; $json = array();
 foreach($xml->EstablishmentCollection->EstablishmentDetail as $elem) {
 $json[$i]['id'] = (string) $elem->FHRSID;
 $json[$i]['business'] = (string) $elem->BusinessName;
 $json[$i]['address'] = $elem->AddressLine1.', '.$elem->AddressLine2.',     '.$elem->PostCode;
$json[$i]['rating'] = (string) $elem->RatingValue;
$json[$i]['date'] = (string) $elem->RatingDate;
$i++;
}

 $numpages = (int) ((sizeof($json) + 39) / 40);

 // Output the JSON encoding of $len elements of the array, starting at $start
 // Stop if we hit the end of the array
 function generate($start, $len) {
 global $json;

 echo('[');
 for($i = $start; $i < ($start + $len); $i++) {
     if(!array_key_exists($i, $json)) break;    // stop at end of array
     if($i != $start) echo(',');
     echo(json_encode($json[$i]));
 }
 echo(']');
 }

 // Search the business names in the array and output the JSON version of any matches
// Limit 40 matches
 function search($str) {
 global $json;

 $i = 0;

 echo('[');
 foreach($json as $elem) {
     if(strpos($elem['business'], $str) !== false) {
         // matches, encode it
         if($i != 0) echo(',');
         echo(json_encode($elem));
         $i++;
         if($i == 40) break;    // enough
     }
 }
 echo(']');
 }

 // Default operation
 if(empty($_GET['op'])) {
generate(0, 10);
return;
}

$op = $_GET['op'];
if($op == 'demo') {
     generate(0,10);
} elseif($op == 'pages') {
 echo('{"pages": '.$numpages.'}');
 } elseif($op == 'retrieve') {
 if(empty($_GET['page'])) die('Retrieve with no page number');
 $pageno = (int) $_GET['page'];
 if(($pageno < 1) || ($pageno > $numpages)) die('Page requested out of range: '.$pageno);
 generate(40 * ($pageno - 1), 40);
 } elseif($op == 'searchname') {
 if(empty($_GET['name'])) die('Empty search string');
 search($_GET['name']);
 } else die('Unknown op: '.$op);
 ?>