在应用程序中有下面提到的PHP形式:
<html>
<body>
<br/>
<br><section style="min-width: 500px; margin: 1px auto;">
<form align="centre" name="showq" id="showq" method="post" action=demo.php>
<fieldset><legend> Demo</legend>
<table>
<tr><td id="one" colspan="2"><div>ABCD</div></td></tr>
<tr><td id="two" colspan="2"><div>EFGH</div></td></tr>
<tr><td></td><td><input type="submit" name="pre" id="pre" value="pre" ></input>
<input type="submit" name="nxt" id="nxt" value="nxt"></input>
<input type="submit" name="getvalfromserver" id="getvalfromserver" value="GETVALFRMSERVER"></input></td><td></td></tr>
</table>
</form></fieldset></section>
</body>
</html>
目前每次点击“pre”或“nxt”或“getvalfromserver”时,它都会提交PHP表单并刷新整个页面并填充<TD>
中id为“one”和“two”的值
数据作为二维PHP数组存储在服务器上
Ex:
Array {
ABCD, EFGH - *First array element*
IJKL, MNOP
QRST, UVWX
and so on...
}
我想避免这个页面刷新;在阅读stackoverflow中的其他帖子后;我想到了两个选择:
在我的情况下,PHP数组的大小可以达到2 MB的数据。用户同时非常频繁地单击“nxt”“pre”导航。
除了上面提到的两个选项之外,还有第三个选项吗? 如果没有,你能否建议;有两种选择;哪一个更适合从绩效角度来看?
答案 0 :(得分:1)
根据您使用的应用程序服务器,您可以轻松打开JSON上的gzip压缩并将大小缩小到600 KB(我假设这是所有文本)
我们已经在apache上完成了这项工作,并且在我们的一个页面上显着提升了性能,没有刷新ExtJS应用程序
1)你首先要对二维数组进行json_encode。
2)然后使用mod_deflate模块在apache服务器上启用压缩 http://httpd.apache.org/docs/2.2/mod/mod_deflate.html
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/x-javascript application/javascript application/xml application/xhtml+xml application/json
3)在胖客户端,您可以使用JSON.parse()或类似功能来解析数据
答案 1 :(得分:1)
听起来你真正需要做的就是创建一种分页数据的方法。特别是因为您似乎一次只需要几个数据点。所以假设您的数组看起来像这样:
Array(
[0] => Array ('ABCD', 'EFGH'),
[1] => Array ('IJKL', 'MNOP'),
...
[n] => Array ('foo', 'bar')
)
你已经非常好地进行了分页。您可以通过执行以下操作轻松确定页数:
$array_size = count($array);
您还可以单独检索任何给定的值数组,直接转到它的索引。所以,假设我想要这个数据集的第100页(如果我按下nxt
按钮99次就会发生这种情况。您可以按照以下方式轻松获得:
// here we assume this page value would be posted by AJAX as parameter 'page'
// note: GET may also be suitable as method here
// note: you would also probably need to validate this is an integer like value that is passed
$page_index = $_POST['page'] - 1;
// get array at this index value
$page_to_return = $array[$page_index];
// encode to JSON to return to calling script
echo json_encode($page_to_return);
这意味着在javascript中您只需要知道您所在页面的值,这样当请求上一页或下一页时,您可以递减/递增该值,将其传递给PHP脚本,然后获取“页面“在该索引处更新到UI。
这允许您在任何给定的页面请求中仅传递数组的一小部分。您甚至可以在javascript中缓存这些值,以便用户前后相同的“页面”不需要额外调用服务器。或者根据您的用户行为,您甚至可以在首页加载时在javascript中预加载前X个页面,然后根据需要请求其他页面。