0

我正在发出 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"}}
4

1 回答 1

0

如果其中一个查询没有结果,则数组中有一个“洞”:

q0、q1、q3、q4

假设,q0 没有结果集,您的数组看起来像

array (
1: result ...,
2: result ...,
3: result ...,
4: result ...,
)

索引 0 未定义。据此生成的 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'];
于 2013-05-28T17:03:53.323 回答