2015-05-19 31 views
-1

我正在迴應來自PHP的兩個數組值。如何區分ajax中的這些值。 如何在Ajax成功函數中區分PHP中的數組值?

if(@mysql_select_db("trainer_registration")) 
{ 

    $select_query_num = @mysql_query("select program_id,facilitator_id,availability_status from program_facilitator"); 
    $select_query_name = @mysql_query("select facilitator_id,firstname,lastname,email_id from facilitator_details"); 
    $num_rows = @mysql_num_rows($select_query_num); 
    $trainerdetails = []; 
    $traineravaildetails = []; 
    $i = 0; 
    while($row = @mysql_fetch_assoc($select_query_num)) 
    { 

     $trainerdetails[$i]['pgidi'] = $row['program_id']; 
     $trainerdetails[$i]['facilitatorid'] = $row['facilitator_id']; 
     $trainerdetails[$i]['avail_status'] = $row['availability_status']; 
     $trainerdetails[$i]['idi'] = $row['facilitator_id']; 

     $i++; 
    } 

     while($row1 [email protected]_fetch_assoc($select_query_name)) 
    { 
     $traineravaildetails[$i]['facilitatorid'] = $row1['facilitator_id']; 
     $traineravaildetails[$i]['firstname'] = $row1['firstname']; 
     $traineravaildetails[$i]['lastname'] = $row1['lastname']; 
     $traineravaildetails[$i]['emailidvalue'] = $row1['email_id']; 
     $i++; 
    } 

    echo json_encode($trainerdetails); 
    echo json_encode($traineravaildetails); 
} 
?> 
function loadavailabletrainers (m) { 
    $.ajax({ 
     url: 'assignavailtrainers.php', 
     data: { action:'test' }, 
     type: 'post', 
     success: function(output) { 
      console.log(output); 
     } 
    }); 
} 

我已經看到了多個返回值的例子來自PHP和AJAX中處理它們,但我不明白他們。有人可以解釋如何區分我的情況下的輸出值嗎?

OUTPUT:

[[{"pgidi":"3","facilitatorid":"2","avail_status":"1","idi":"2"},{"pgidi":"3","facilitatorid":"1","avail_status":"2","idi":"1"},{"pgidi":"3","facilitatorid":"2","avail_status":"1","idi":"2"},{"pgidi":"3","facilitatorid":"1","avail_status":"2","idi":"1"},{"pgidi":"3","facilitatorid":"2","avail_status":"1","idi":"2"},{"pgidi":"3","facilitatorid":"2","avail_status":"1","idi":"2"},{"pgidi":"3","facilitatorid":"2","avail_status":"2","idi":"2"}],{"7":{"facilitatorid":"1","firstname":"Vignesh","lastname":"Anand","emailidvalue":"v*******@gmail.com"},"8":{"facilitatorid":"2","firstname":"Vignesh","lastname":"Anandakumar","emailidvalue":"vign*****@gmail.com"},"9":{"facilitatorid":"3","firstname":"Vignesh","lastname":"Anand","emailidvalue":"v*****@hotmail.com"},"10":{"facilitatorid":"4","firstname":"Vignesh","lastname":"Anand","emailidvalue":"****@live.com"}}] 
+0

發佈輸出值 –

+0

發佈輸出值。 –

+1

自己試試吧: 'output [0]' – xNeyte

回答

1

這是一個很好的做法是隻發送一個值的流這樣你就可以同時處理這一切。

首先,你可以創建一個容器數組:

$data = array('trainerdetails'  => $trainerdetails, 
       'traineravaildetails' => $traineravaildetails); 

然後

echo json_enconde($data); 

,這將產生一個合併輸出。

您的PHP代碼返回的編碼字符串需要在客戶端解碼(更多詳細信息:Parse JSON in JavaScript?)。因此,您可以使用$ .getJSON(),它是對$ .ajax(doc:http://api.jquery.com/jquery.getjson/)的特定調用的別名。

「成功」函數將傳遞'key'=>'value'數組數據。在這種情況下,您需要將值視爲可能包含額外級別的數組。如果您可以將數據結構可視化爲樹形視圖,則可以提供幫助,例如:http://jsonviewer.stack.hu/(將輸出粘貼到那裏)。

我希望它有幫助!

相關問題