我正在发出一个AJAX请求并试图解析返回的数据。我的$echoArray
似乎没有在每次通话时清空,但正在递增。我每次都需要一个新阵列。我也无法访问返回数据中的值。
如何在脚本中重新初始化数组,以便只返回新数据并且索引不会递增?那我怎么访问这些数据呢?
AJAX致电:
$j.ajax({
type: "POST",
url: "/ajax_calls/updatePrices.php",
data: { 'vendorID': IDs, 'product_id': product_id}
}).done(function(data) {
//var data= JSON.parse(data);
console.log('The data is ' + data);
console.log('Range start is data[1].tier2_range_start: ' + data[1].tier2_range_start);
//console.log('Range start is data.tier2_range_start: ' + data.tier2_range_start);
console.log('Range start is data[0].tier2_range_start: ' + data[0].tier2_range_start);
console.log('Range start is data.tier2_range_start[1]: ' + data.tier2_range_start[1]);
console.log('Range start is data.tier2_range_start[0]: ' + data.tier2_range_start[0]);
for(i=0; i<IDs.length; i++)
{
var vendor = IDs[i];
var basePrice = simpleArray[vendor][colorSelected];
if (qty < data.tier2_range_start){
simpleArray[vendor][colorSelected]= basePrice * qty;
}
else if (qty > data.tier2_range_start){
simpleArray[vendor][colorSelected]= (basePrice * qty) * data.tier2_discount;
}
else if (qty > data.tier3_range_start){
simpleArray[vendor][colorSelected]= (basePrice * qty) * data.tier3_discount;
}
else if (qty > data.tier4_range_start){
simpleArray[vendor][colorSelected]= (basePrice * qty) * data.tier4_discount;
}
else if (qty > data.tier5_range_start){
simpleArray[vendor][colorSelected]= (basePrice * qty) * data.tier5_discount;
}
else{
console.log('Something went wrong');
}
}
$j('.details'+vendor+ ' .priceBlock').empty();
$j('.details'+vendor+ ' .priceBlock').append('<span>'+simpleArray[vendor][colorSelected]+'</span>');
});
脚本调用:
<?php
require_once('/var/www/Staging/connect.php');
//post variable
$IDArray= $_POST['vendorID'];
$product_id= $_POST['product_id'];
$echoArray= array();
for($x=0; $x< sizeof($IDArray); $x++) //Query for each vendor ID in array
{
$sql = 'SELECT * FROM tier_pricing WHERE vendor_id=' . $IDArray[$x]. ' AND product_id=' . $product_id;
foreach ($con->query($sql) as $row) {
$echoArray[$x]['vendor_id']= $row['vendor_id'];
$echoArray[$x]['tier2_range_start']= $row['tier2_range_start'];
$echoArray[$x]['tier2_range_stop']= $row['tier2_range_stop'];
$echoArray[$x]['tier3_range_start']= $row['tier3_range_start'];
$echoArray[$x]['tier3_range_stop']= $row['tier3_range_stop'];
$echoArray[$x]['tier4_range_start']= $row['tier4_range_start'];
$echoArray[$x]['tier4_range_stop']= $row['tier4_range_stop'];
$echoArray[$x]['tier5_range_start']= $row['tier5_range_start'];
$echoArray[$x]['tier2_discount']= $row['tier2_discount'];
$echoArray[$x]['tier3_discount']= $row['tier3_discount'];
$echoArray[$x]['tier4_discount']= $row['tier4_discount'];
$echoArray[$x]['tier5_discount']= $row['tier5_discount'];
}
}
echo json_encode($echoArray);
?>
CONSOLE.LOG(数据):
查看递增。这是在对脚本进行两次AJAX调用之后。当它进入第三次调用时,它返回一组空的数据[]:
[11:54:52.090] The data is {"1":{"vendor_id":"3","tier2_range_start":"5","tier2_range_stop":"20","tier3_range_start":"20","tier3_range_stop":"100","tier4_range_start":"100","tier4_range_stop":"500","tier5_range_start":"500","tier2_discount":"2","tier3_discount":"3.1","tier4_discount":"4.3","tier5_discount":"5"}}
[11:55:14.002] The data is {"2":{"vendor_id":"3","tier2_range_start":"5","tier2_range_stop":"20","tier3_range_start":"20","tier3_range_stop":"100","tier4_range_start":"100","tier4_range_stop":"500","tier5_range_start":"500","tier2_discount":"2","tier3_discount":"3.1","tier4_discount":"4.3","tier5_discount":"5"}}
答案 0 :(得分:0)
如果其中一个查询没有结果,那么数组中就会出现“漏洞”:
q0,q1,q3,q4
假设,q0没有结果集,你的数组看起来像
array (
1: result ...,
2: result ...,
3: result ...,
4: result ...,
)
索引0未定义。根据{{3}},生成的json将是一个对象而不是一个数组。
您应该在任何情况下初始化数组值: (让PHP执行自动初始化的不良行为)
$sql = 'SELECT * FROM tier_pricing WHERE vendor_....
if ( !isset($echoArray[$x]) )
$echoArray[$x] = array();
foreach ($con->query($sql) as $row) {
$echoArray[$x]['vendor_id']= $row['vendor_id'];