2012-04-05 68 views
0

如何修改我現有的JQuery代碼以循環訪問數組的JSON數組?例如,當PHP從包含多於一行的MySQL數據庫返回JSON結果時。JSON。分析數組的數組。怎麼樣?

這是我的代碼。它只適用於單行結果。

$(function(){ 

    $('#btn_select_account').live('click', function() { 

    // URL... 
    $.getJSON('api.php?', 
    // Parameters... 
    { call: 'select_account', p0: 'suchislife801' }, 

    function(result){ 

     // For each item inside array... 
     $.each(result, function(index, value) { 

      // Append to this html element 
      $('#output').append(index + ': ' + value + '<br />').fadeIn(300); 

     }); 

    }); 

    }); 

}); 

下面的PHP代碼....

function qry_select_account($pk_account) { 

    // Global variables 
    Global $db_host, $db_user, $db_pass, $db_name; 

    // Connect to database server 
    $dbc = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error()); 
    // Select target database 
    mysql_select_db($db_name) or die(mysql_error()); 

    // suchislife801 <--- Selects account information 
    // Run query 
    $sql_qry = mysql_query("SELECT * FROM tblaccount 
         WHERE pk_account = '$pk_account'") or die(mysql_error()); 

    // SQL Criteria AND acc_confirmed = 'y' AND acc_locked = 'n' 

    // Fetch table row for this user 
    $row = mysql_fetch_row($sql_qry); 

    print json_encode($row); 

    mysql_free_result($sql_qry); 

    // Close Connection 
    mysql_close($dbc); 

    } 

產生以下反應:

["20","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:38","n","0"] 

我試圖去改變它,使之與下面的PHP代碼工作。 ...

function qry_select_last5_entries_for_user($ent_user) { 

    // Global variables 
    Global $db_host, $db_user, $db_pass, $db_name; 

    // Connect to database server 
    $dbc = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error()); 
    // Select target database 
    mysql_select_db($db_name) or die(mysql_error()); 

    // suchislife801 <--- Selects last 5 entries for user 
    // Run query 
    $sql_qry = mysql_query("SELECT * FROM tblentry 
          WHERE ent_user = '$ent_user' ORDER BY ent_date DESC , ent_time DESC LIMIT 5") or die(mysql_error()); 

    // Fetch table rows for this user 
    while ($row = mysql_fetch_array($sql_qry, MYSQL_NUM)) { 

    print json_encode($row); 

    } 

    mysql_free_result($sql_qry); 

    // Close Connection 
    mysql_close($dbc); 

    } 















["20","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:38","n","0"]["19","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:37","n","0"]["18","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:36","n","0"]["17","1","suchislife801","Ugly","My first entry title.","Body of my first entry.","2012-04-03","15:06:35","n","0"]["15","1","suchislife801","Lazy","My first entry title.","Body of my first entry.","2012-04-03","15:06:34","n","0"] 
+0

你可以發佈json輸出的樣本,或者可能是生成它的php嗎? – 2012-04-05 14:34:50

+0

我剛剛爲您添加了樣本。 – ASPiRE 2012-04-05 15:26:37

+0

你的php似乎沒有返回有效的json。 – 2012-04-05 15:41:51

回答

2

在你的PHP中,修改你的循環來創建一個數組,然後回顯該數組的json_encode結果。

輸出應該是這樣的:

[["20","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:38","n","0"],["19","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:37","n","0"],["18","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:36","n","0"],["17","1","suchislife801","Ugly","My first entry title.","Body of my first entry.","2012-04-03","15:06:35","n","0"],["15","1","suchislife801","Lazy","My first entry title.","Body of my first entry.","2012-04-03","15:06:34","n","0"]] 

然後,在jQuery中,你會輸出這樣的:

var outHtml = ""; 
$.each(result, function(index, value) { 
    outHtml += "Entry " + index + ":<br />"; 
    outHtml += $.map(value,function(i,value){ 
     return i + ": " + value + "<br />"; 
    }).join(""); 
    outHtml += "<br /><br />"; 
}); 

$("#output").html(outHtml); 

演示:http://jsfiddle.net/R8wvn/

編輯:
PHP獲得所需的資源ult(未測試):

var $out = array(); 
// Fetch table rows for this user 
while ($row = mysql_fetch_array($sql_qry, MYSQL_NUM)) { 
    array_push($out,$row); 
} 
print json_encode($out); 
+0

呃,我明白你的意思了。while循環正在生成1個數組。它需要改爲創建一個數組數組,並將其編碼爲JSON。 – ASPiRE 2012-04-05 16:18:41

0

因爲它lo OKS像value將是一個JSON對象,以及,你可以這樣做:

$.each(result, function(index, value) { 
    for (var key in value) 
     $('#output').append(index + ': ' + key + ': ' + value[key] + '<br />').fadeIn(300); 
)}; 

小提琴演示:http://jsfiddle.net/StefanCoetzee/FZ8f2/3/

編輯:只是改變你的PHP代碼如下:

$arr_results = array(); 
// Fetch table rows for this user 
while ($row = mysql_fetch_array($sql_qry, MYSQL_NUM)) 
    $arr_results[] = $row;   // quicker than using array_push() 
echo json_encode($arr_results); // *echo* is apparently slightly faster than *print* 

此結果在有效的json輸出中。