带有JSON数据的动态表

时间:2013-10-28 17:48:17

标签: php jquery json

我有一个用jquery创建的表,但我无法获取对象名称。我可以获得值并显示它很好,但由于某种原因,没有返回对象名称/标签。

我的HTML

$('.compare-link a').on('click', function(e){
        e.preventDefault();
        $.ajax({
            type: "GET",
            url: "http://localhost:8888/wp-content/themes/wp-foundation/load-products.php",
            dataType: 'json',
            data: {permalink: $(this).data('device-compare')},
            beforeSend: function() {
                $('.compare-devices').html("<div class='loader'><img src=" + root + "/wp-content/themes/wp-foundation/images/ajax-loader.gif /></div>");
            },
            success: function(data) {
                var table = $("<table>").attr('id', 'compare').attr('class', 'device-compare'),
                cont = $('<div class="row container"><div class="twelve columns text"><h2>Compare Phones &amp; Devices</h2></div><div class="twelve columns price-table">'),
                contEnd = $('</div></div>');
                $('.compare-devices').append(cont);
                $('.price-table').prepend(table);
                for (var i in data) {
                     var tr="<tr>";
                     var td1="<td>"+data[i]+"</td>"; //this should be the json object/label
                     var td2="<td>"+data[i]["device_retail_price"]+"</td>";
                    $('#compare').append(tr+td1+td2);
                }
                $('.compare-devices').prepend(contEnd);
            }
        });
    });

我的PHP

<?php

$compare = $_GET['permalink'];

$link = mysql_pconnect("localhost", "user", "pass") or die("Could not connect");
mysql_select_db("dbname") or die("Could not select database");

$arr = array();

$query = mysql_query("SELECT columns FROM table WHERE permalink ='" . $compare . "'");

while($obj = mysql_fetch_assoc($query)) 
{
$arr[] = $obj;
}

foreach ($arr as $innerArray) {
foreach ($innerArray as $key => $value) {
    $arr[$key][] = $value;
}
}

echo '{"devices":'. json_encode($arr). '}';

?>

我的JSON响应

devices: {
0: {
    name: Samsung Galaxy S4,
    device_manufacturer: Samsung,
    device_model: Galaxy S4,
    …
    },
    …
}
0: {
    name: Samsung Galaxy S4,
    device_manufacturer: Samsung,
    device_model: Galaxy S4,
    …
} 
device_2_yr_contract_price: [249.99]
device_charging_port_type: []
device_external_storage: [Up to 64GB MicroSD]
device_front_facing_camera: [2MP]
device_hac_rating: [M3]
device_internal_storage: [16GB]
device_manufacturer: [Samsung]
device_model: [Galaxy S4]
device_os_type: [Android 4.2.2(Jelly Bean)]
device_processor: [1.9GHz, Quad - core Processor]
device_ram: [2 GB]
device_rear_facing_camera: [13MP w / LED Flash]
device_retail_price: [719.99]
device_screen_size: [null]
device_wi_fi_hotspot: [1]
name: [Samsung Galaxy S4]

那我在json对象标签上出错了?任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

仅打印i而不是data[i]

这是一个小提琴样本:http://jsfiddle.net/snowburnt/FaAWj/

对于您想要做的事情,您可以这样做:

for(i in data.devices){
    alert (i);//should be name, then device_manufacturer, etc
    alert(data.devices[i]); //should be value
}