我们希望在数据表中存储大量数据(超过60K行)但数据表无法执行并且输出效果不佳。数据表在20000条记录之前不会失败。以下是源代码
<div class="row">
<table id="customer_table" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>#</th>
<th>Customer Name</th>
<th>Email</th>
<th>Group</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php for($p=1;$p<=200;$p++)
{
$ch = curl_init("https://$shop_name/admin/customers.json?limit=250&page=".$p."");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json",
"X-Shopify-Access-Token: $store_token"));
$result = curl_exec($ch);
$decoded_customer = json_decode($result, true);
//var_dump($decoded_customer);
?>
<?php for($i=0;$i<count($decoded_customer['customers']);$i++)
{
?>
<tr>
<input type="hidden" value="<?php echo $decoded_customer['customers'][$i]['id']; ?>" >
<td>#</td>
<td><?php echo $decoded_customer['customers'][$i]['first_name']." ".$decoded_customer['customers'][$i]['last_name'];?></td>
<td><?php echo $decoded_customer['customers'][$i]['email']; ?></td>
<td>
<select class="form-control" >
<option value="" >
No Selected
</option>
<?php
session_start();
$shop_name = $_SESSION['shop'];
include("dbconnect.php");
$sql_select_tbl_shop = "SELECT * FROM `store_info_tbl` WHERE `store_name` = '$shop_name'";
$select_tbl_shop = mysql_query($sql_select_tbl_shop);
if(mysql_num_rows($select_tbl_shop)>0){
$row_select_val=mysql_fetch_array($select_tbl_shop );
$shop_id=$row_select_val['id'];
//select selected group name
$customer_id=$decoded_customer['customers'][$i]['id'];
$sqlselectgroup_customer="SELECT * FROM `tbl_group` WHERE `store_name`='$shop_id'";
$resselectgroup_customer=mysql_query($sqlselectgroup_customer);
if(mysql_num_rows($resselectgroup_customer))
{
while($rowselectgroup_customer=mysql_fetch_assoc($resselectgroup_customer))
{
$sqlselectcustomergroup="SELECT * FROM `tbl_customer_detail` WHERE `customer_shop_id`='$customer_id'";
$resselectcustomergroup=mysql_query($sqlselectcustomergroup);
if(mysql_num_rows($resselectcustomergroup)>0){
//$rowselectcustomer=mysql_num_rows($resselectcustomergroup);
//$rowid=mysql_fetch_array('group_name');
$rowid=mysql_fetch_array($resselectcustomergroup);
$customer_group=$rowid['group_name'];
?>
<option value="<?php echo $rowselectgroup_customer['id']; ?>" <?php if($customer_group==$rowselectgroup_customer['id']) { ?>selected <?php } ?> >
<?php echo $rowselectgroup_customer['group_name']; ?>
</option>
<?php } else{ ?>
<option value="<?php echo $rowselectgroup_customer['id']; ?>" >
<?php echo $rowselectgroup_customer['group_name']; ?>
</option>
<?php } } } } ?>
</select>
</td>
<td><button type="button" class="btn btn-primary save">Save</button></td>
</tr>
<?php } } ?>
</tbody>
</table>
<script type="text/javascript" >
$(document).ready(function(){
$('#customer_table').DataTable();
});
$(document).on('click','.save',function(){
var shopify_customer_id=$(this).parent().parent().find('input').val();
var customer_name=$(this).parent().parent().find('td').eq(1).html();
var customer_email=$(this).parent().parent().find('td').eq(2).html();
var group_name=$(this).parent().parent().find('td').find('select').val();
//alert(shopify_customer_id+customer_name+customer_email+group_name);
$.ajax({
type: "post",
url: "ajaxcustomerdata.php",
data: {
shopify_customer_id:shopify_customer_id,
customer_name:customer_name,
customer_email:customer_email,
group_name:group_name
},
success: function (data){
alert(data);
},
error: function (xhr, ajaxOptions, thrownError){
}
});
});
</script>
<script>
</script>
</div>
如果有任何人提出任何建议,请帮我解决此问题
答案 0 :(得分:1)
请从数据表中阅读https://datatables.net/extensions/scroller/examples/initialisation/server-side_processing.html官方帖子。它显示了如何在数据表中添加数千条记录。
以下是此示例。
$(document).ready(function() {
$('#example').DataTable( {
serverSide: true,
ordering: false,
searching: false,
ajax: function ( data, callback, settings ) {
var out = [];
for ( var i=data.start, ien=data.start+data.length ; i<ien ; i++ ) {
out.push( [ i+'-1', i+'-2', i+'-3', i+'-4', i+'-5' ] );
}
setTimeout( function () {
callback( {
draw: data.draw,
data: out,
recordsTotal: 160421,
recordsFiltered: 160421
} );
}, 50 );
},
scrollY: 600,
scroller: {
loadingIndicator: true
}
} );
} );
数据表的html:
<table id="example" class="display nowrap" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>ZIP / Post code</th>
<th>Country</th>
</tr>
</thead>
</table>
答案 1 :(得分:0)
虽然您不应该一次加载这么多记录,但它会使您的服务器不安全,如果一次发出多个请求,您可能会耗尽资源。您应该尝试基于Ajax的Datatable。
https://datatables.net/examples/ajax/objects.html
上面的链接使用AJAX调用来加载数据,这样您就不必查询所有60,000行。