weixin_33749242 2015-12-12 10:03 采纳率: 0%
浏览 11

jQuery URL分页

I am using jQuery and AJAX to fetch some data from a PHP file which is stored remotely on a server. I was able to fetch the first page and display the results in an HTML table. I am facing another issue now: I am trying to fetch all the pages and then make a pagination for all of these pages.

Here is the code I have so far:

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

This is the PHP script:

<?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);
 ?>
  • 写回答

1条回答 默认 最新

  • weixin_33674976 2015-12-12 10:55
    关注

    since you are using ajax, you could load only the requested pages instead of all the pages once.

    You can retrieve the number of pages using hygiene.php?op=pages

    then you know how many pages you'll have to display, you can display your pages link and load the first page.

    to load a page : hygiene.php?op=retrieve&page=1

    or page number 2 for example hygiene.php?op=retrieve&page=2

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题