0

I have a json encoded array, which can change based on how many are returned from my query in my php code:

$mysqli = new mysqli('test', 'root', '', 'test');
$mysqli->set_charset('utf8');
$array = array();

$query = $mysqli->query("SELECT * FROM test.table1 WHERE id = '$id'");
    while($row = $query->fetch_assoc()) {
        $array[] = $row['char'];
    }
json_encode($array);

example:
array("a", "b", "c", "d"); or
array("a", "b", "c", "d", "e"); or
array("a", "b", "c", "d", "e", "f");

I was wondering how to display the array line by line

example:

a

b

c

d

$.ajax({
    url: code.php,
    data: {
          id: id,
    },
    dataType: 'json',
    cache: false,
    success: function(d) {
        $('#code').text(d);
    },
    error: function() {
    }
});

I know the above ajax code is wrong but how can I make it right?

4

2 回答 2

0
success: function(d) {
    var htmlstring='';
    $.each(d,function(i,e) {htmlstring+=e+'<br/>';});
    $('#code').text(htmlstring);
},
于 2013-11-08T08:02:49.893 回答
0

另一种方式,但将其显示为无序列表:

success: function(d) {
    var ul = $('<ul>').appendTo('#code')
    var arr = JSON.parse(d),
    var len = arr.length;
    while(len--) $('<li>', { text: arr[len] }).appendTo(ul);
}
于 2013-11-08T08:13:02.740 回答