2011-04-12 95 views
0

PHP server side我有這些值想要傳回JQuery來更新客戶端信息。從PHP通過JSON將多值傳遞給JQuery的方法?

如:

fileSN fileTotalNumber fileTotalSize 
1  3    455 
2  4    555 
3  1    755 
4  1    155 

的PHP代碼來獲得每一個數據:

$fileData = array(); 

while($row = mysqli_fetch_array($load_files_data_result, MYSQL_ASSOC)){ 

     $fileSN = $row['file_sn']; 
     $fileTotalNumber = $row['fileTotalNumber ']; 
     $fileTotalSize= $row['fileTotalNumber ']; 

     // only one data here , and need more data associated with fileSN 
     $fileData[$fileSN] = $fileTotalNumber; 

    } 

return json_encode($fileIds); 

,並在客戶端,我的HTML代碼:

<ul> 
    <li fileSN='1' fileTotalNumber='3' fileTotalSize='345'>update li information</li> 
    ... 
</ul> 

我的jQuery代碼:

$.ajax({ 
    ... 
    success: function(result){     
    result = jQuery.parseJSON(result); 

    $.each(result, function(index, value) { 

     // need more values to update other attributes 

     $('#'+obj.FMMWID+'UL li[fileSN='+index+']') 
     .addClass('igtomdSelectedItem') 
     .attr('allfiles',value); 
     //console.log(value) 
    }); 
    } 
}); 

非常感謝!

+0

您將不得不在服務器端和客戶端上echo echo json_encode($ data)'做一個啓用了數據類型JSON的'$ .aja'x調用。 – kjy112 2011-04-12 15:25:43

+0

是的。我已經完成了。我需要一個數據結構來包裝我想要的所有數據並將它們傳回給JQuery! – qinHaiXiang 2011-04-12 15:33:04

回答

0

如果你創建喜歡你的表信息的關聯數組:

$tableData[$rownumber]['field1'] = 'fieldvalue'; 
$tableData[$rownumber]['field2'] = '2ndFieldValue'; 

而做到這一點對所有的行和字段,然後你可以編碼使用數組json_encode()。

所以,如果你的數據返回給jQuery的作爲data你就可以訪問這樣的東西:

data[0].field1 

其中0是一個有效的行號,FIELD1是陣列領域之一。

+0

It works.Thank you very much !! – qinHaiXiang 2011-04-12 16:04:20