作者cold514 (路人)
看板Ajax
標題[問題] json 回傳多個值
時間Fri May 31 18:23:11 2013
我想從資料庫撈資料(多筆)放回頁面
無奈小弟不會用json 得到的都是object Object
懇請各位前輩賜教 或是提供關鍵字 ~"~
以下是用jQuery autocomplete改的
http://jqueryui.com/autocomplete/#remote
----------search.php----------
$stmt = $conn->prepare('
select id,name from table');
$stmt->execute();
while($row = $stmt->fetch()) {
$return_arr[] = $row;
}
echo json_encode($return_arr);
------------------------------
會得到多行
example:
[{"id":455,"0":455,"name":"\u6c5f","1":"\u6c5f"},
{"id":393,"0":393,"name":"\u8521","1":"\u8521"}]
----------jQuery----------
$( "#birds" ).autocomplete({
source: "search.php",
minLength: 2,
response: function( event, ui ) {
log( ui );
}
});
function log( message ) {
$( "<div>" ).text( messageitem.value ).prependTo( "#output" );
$( "#output" ).scrollTop( 0 );
}
----------html----------
<div>
<label for="birds">Birds: </label>
<input id="birds" />
</div>
<div>
<div id="output"> Here </div>
</div>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 219.81.228.254
※ 編輯: cold514 來自: 219.81.228.254 (05/31 18:26)
1F:推 kerash:autocomplete 預設回傳的是一維陣列,你這樣就變二維了 05/31 20:22
3F:→ cold514:while($row = $stmt->fetch()) { 05/31 20:53
4F:→ cold514: $return_arr[] = array( 05/31 20:54
5F:→ cold514: 'c_id'=>$row['c_id'], 05/31 20:54
6F:→ cold514: 'name'=>$row['name']); } 05/31 20:54
7F:→ cold514:response: function( event, ui ) { 05/31 20:56
8F:→ cold514: log( ui.content[1].name ); 05/31 20:56
9F:→ cold514:} 05/31 20:56