0

配列のJSON配列をループするように既存のJQueryコードを変更するにはどうすればよいですか?たとえば、PHPが複数の行を含むMySQLデータベースから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"]
4

2 に答える 2

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を使用して目的の結果を取得します(テストされていません):

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);
于 2012-04-05T15:50:31.360 に答える
0

jsonオブジェクトにもなるように見えるので、次のようvalueにすることができます。

$.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出力が得られます。

于 2012-04-05T14:50:58.753 に答える