1

大家好,我需要以下帮助代码,

我正在尝试使用 json 将 2 个不同的结果从 mysql php 提取到 ajax。

这是我目前所拥有的。

我可以提取 1 个包含信息的表格,但不知道如何提取 2 个不同的结果。请帮我。

PHP 文件

    $id = $_POST["order"];

    $order = mysql_query("SELECT * FROM `orders` WHERE `order_id` = {$id} ORDER BY id DESC");

    $test_quote = mysql_query("
                        SELECT paid
                        FROM `outstanding`
                        WHERE order_id = {$id}
                        ");
    $array = array();

//pulling outstanding if product paid or not                    
    if(mysql_num_rows($test_quote) > 0 ){
        $array[] =  mysql_fetch_array($test_quote);
    }



    //pulling products
    if (mysql_num_rows($order) > 0){ 

        while($row = mysql_fetch_array($order)){

            $array[] = $row;

        } 



    } else { echo "<div style='font-size:12px;'>No orders yet</div>";}

    echo json_encode($array);

它拉 1 个查询,但无法从 php 文件中获取另一个查询

jQuery

            $.ajax({
              type: "POST",
              url: "order_load.php",
              data: {order : thisId},
              dataType:"json",    
              success: function(data){

                  $.each(data, function(i, val){

                      var append2 = '<div class="product">';
                        append2 +='<div class="view_left2">';
                        append2 +='<b>Item ID:</b> ';
                        append2 +='#'+val.id;
                        append2 +='</div>';
                        append2 +='<div class="line"><img src="images/productLine.png" /></div>';
                        append2 +='<div class="viewleft3">'+val.desc+'</div>';
                        append2 +='<div class="right_order">';
                        append2 +='<div class="line"><img src="images/productLine.png" /></div>';
                        append2 +='<div class="view_right2">';
                        append2 +='<div class="price3">$'+val.amount+'</div></div><div class="clear"></div></div>';
                        append2 +='<div class="clear"></div></div>';
                        $(".view_product2").append(append2);
                  });

这是我的 json 输出:

[{"0":"1","paid":"1"},{"0":"336","id":"336","1":"1","qty":"1","2":"sdfgsdf","desc":"sdfgsdf","3":"yes","gst":"yes","4":"44","price":"44","5":"44.00","amount":"44.00","6":"225","order_id":"225"}]
4

1 回答 1

1

您的代码有几处问题。首先; 您从返回的 json 中引用了一个不存在的 json 属性,.amount. 我已将其更改.price为返回正确的结果。

第二..

您返回的 json 是一个对象数组。要访问该.paid属性,您需要先引用数组的第一项,然后才能访问和设置paid变量..这是您从未做过的。

var paid = data[0].paid; // set the paid variable per its placement in the json

$.each(data, function (i, val) {
    if (i != 0) { // check to make sure it isn't the first object of the array
        var append2 = '<div class="product">';
        append2 += '<div class="view_left2">';
        append2 += '<b>Item ID:</b> ';
        append2 += '#' + val.id;
        append2 += '</div>';
        append2 += '<div class="line"><img src="images/productLine.png" /></div>';
        append2 += '<div class="viewleft3">' + val.desc + '</div>';
        append2 += '<div class="right_order">';
        append2 += '<div class="line"><img src="images/productLine.png" /></div>';
        append2 += '<div class="view_right2">';
        append2 += '<div class="price3">$' + val.price + '</div></div><div class="clear"></div></div>';
        //                                       ^ use the correct json property to access the value.  It is not amount.
        append2 += '<div class="clear"></div></div>';
        $(".view_product2").append(append2);
    }
});
于 2013-05-04T06:28:03.443 回答