Ajax,jquery,PHP在迭代中加载数据 [英] Ajax, jquery, PHP load data in iteration
本文介绍了Ajax,jquery,PHP在迭代中加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
与AJAX / PHP相关的问题,是否有一种方法可以一次列出5000条记录,而不是APPEND下一条5000条记录在HTML表格中等等,直到使用ajax结束记录 - php
单击HTML按钮加载脚本:或者document.ready()并显示进度条。
我尝试过:
AJAX/PHP related question, is there a way to list 5000 records at a time and than APPEND next 5000 records in HTML Table and so on until end of records using ajax - php
Load Script on click of a HTML Button: OR document.ready() and also show progress bar.
What I have tried:
// counter that allows you to get a new set of rows
var step = 0;
// set variable if you want to restrict the number of rows will be loaded
var maxStep = 0;//
// how many rows should be returned
var count = 5000;
// if the cancel button is pressed
var cancel = false;
$(function() {
$('#load').click(function(){
getData();
})
$('#cancel').click(function(){
cancel = true;
})
});
function getData()
{
step++;
//If cancel variable is set to true stop new calls
if(cancel == true) return;
// checks if the variable is set and limits how many rows to be fetched
if(maxStep >0 and step >= maxStep) return;
$.post('ajax.php'
,{
'step':step,
'count':count,
}
,function(data, textStatus, jqXHR){
if(textStatus == "success")
foreach ($data as $row) {
echo $row['username']." -- ID :" .$row['user_id']. " -- FirstName :" .$row['first_name']. "<br>\n";
}
if(textStatus == "error")
alert("Error: " + jqXHR.status + ": " + jqXHR.statusText);
// when it finishes processing the data, call back function
getData();
}
,'json'
)
}
==== ajax.php =====
step = 0;
if(isset($_POST['step'])) $step = (int)$_POST['step'];
$count = 0;
if(isset($_POST['count'])) $count = (int)$_POST['count'];
if($step>0 and $count>0)
{
$offset = ($step-1) * $count;
$limit = $offset.','.$count;
// --------------
// your code here
// --------------
$data = $DB->query("SELECT * FROM user_details LIMIT .$limit")->fetchAll();
$result = mysql_query($sql);
$arr_result = array();
foreach ($data as $row) {
$arr_result[] = $row;
}
$arr_result_enc = json_encode($arr_result);
echo $arr_result_enc;
// echo rows
//echo json_encode($rows);
}
推荐答案
(function(){
(function() {
('#load' ).click(function(){
getData();
})
('#load').click(function(){ getData(); })
('#cancel')。click(function(){
cancel = true;
})
});
函数getData()
{
step ++;
//如果取消变量设置为true,则停止新调用
if(cancel == true)return;
//检查变量是否设置并限制要提取的行数
if(maxStep> 0和step> = maxStep)return;
('#cancel').click(function(){ cancel = true; }) }); function getData() { step++; //If cancel variable is set to true stop new calls if(cancel == true) return; // checks if the variable is set and limits how many rows to be fetched if(maxStep >0 and step >= maxStep) return;
这篇关于Ajax,jquery,PHP在迭代中加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文