如何使用AJAX和json从PHP Mysql中提取2个结果

时间:2013-05-04 05:31:20

标签: php jquery mysql ajax json

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

我试图使用json将两个不同的结果从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"}]

1 个答案:

答案 0 :(得分:1)

您的代码有几个问题。首先;你从你返回的json .amount引用一个不存在的json属性。我已将其更改为.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);
    }
});