簡體   English   中英

具有JSON數據的動態表

[英]Dynamic Table with JSON data

我有一個用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對象標簽哪里出問題了? 任何幫助,將不勝感激。

謝謝

僅打印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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM