2014-04-29 76 views
0

我想顯示我在表中創建的數組的值。我構建的原始解決方案是爲了顯示與數組中的ID完全匹配的數組的單個值。你可以在(http://www.users.miamioh.edu/chaudha/cse252/assignment92/)查看。在「jjones」中鍵入將顯示結果。在表中顯示JSON數組的值

我修改了此解決方案以接受部分查詢以及顯示適合部分查詢(注:http://www.users.miamioh.edu/chaudha/cse252/assignment9/service/people/jjhttp://www.users.miamioh.edu/chaudha/cse252/assignment92/service/people/jj)的每個值。我開始遇到關於將數組解析到表中的問題。最終目標是顯示滿足部分查詢的人員列表。我想弄清楚爲了完成這個,需要在Javascript中修改什麼。

我的JS如下:

$(document).ready(function() { 
$("#searchClassmates").keyup(function(){ 
    var searchValue = document.getElementById('searchClassmates').value; 
    $.getJSON("http://www.users.miamioh.edu/chaudha/CSE252/Assignment9/service/people/" + searchValue, function (json) { 
     for(var i = 0; i < json.length; i++) { 
      $("<tr>").appendTo("#classMateResults"); 
      $("<td>" + json[i].firstName + "</td>").appendTo("#classMateResults"); 
      $("<td>" + json[i].lastName + "</td>").appendTo("#classMateResults"); 
      $("<td>" + json[i].age + "</td>").appendTo("#classMateResults"); 
      $("<td>" + json[i].major + "</td>").appendTo("#classMateResults"); 
      $("<td>" + json[i].phone + "</td>").appendTo("#classMateResults"); 
      $("<td>" + json[i].email + "</td>").appendTo("#classMateResults"); 
      $("<td>" + json[i].state + "</td>").appendTo("#classMateResults"); 
      $("</tr>").appendTo("#classMateResults"); 
     } 
    }); 
    }); 
}); 

表:

$people = array(
'jjones' => array('firstName' => 'Jim', 'lastName' => 'Jones', 'age' => 20, 'major' => 'Computer Science', 'phone' => '212-460-9393', 'email' => '[email protected]', 'state' => 'OH'), 
'asmith' => array('firstName' => 'April', 'lastName' => 'Smith', 'age' => 19, 'major' => 'Mechanical Engineering', 'phone' => '913-939-3929', 'email' => '[email protected]', 'state' => 'WY'), 
'pstemple' => array('firstName' => 'Pat', 'lastName' => 'Stemple', 'age' => 21, 'major' => 'Theater Performance', 'phone' => '917-222-2232', 'email' => '[email protected]', 'state' => 'NY'), 
'jjones1' => array('firstName' => 'Janet', 'lastName' => 'Jones', 'age' => 22, 'major' => 'Botany', 'phone' => '817-332-9392', 'email' => '[email protected]', 'state' => 'CA'), 
'llerner' => array('firstName' => 'Leon', 'lastName' => 'Lerner', 'age' => 18, 'major' => 'Biology', 'phone' => '315-444-3494', 'email' => '[email protected]', 'state' => 'OH'), 
'mmeyer' => array('firstName' => 'Margret', 'lastName' => 'Meyer', 'age' => 24, 'major' => 'Interactive Media Studies', 'phone' => '219-333-0303', 'email' => '[email protected]', 'state' => 'OH'), 
'achaudhry' => array('firstName' => 'Anik', 'lastName' => 'Chaudhry', 'age' => 19, 'major' => 'Management Information Systems', 'phone' => '914-555-5555', 'email' => '[email protected]', 'state' => 'NY'), 
'sdogg' => array('firstName' => 'Snoop', 'lastName' => 'Dogg', 'age' => 42, 'major' => 'Botany', 'phone' => '414-333-2433', 'email' => '[email protected]', 'state' => 'CA'), 
'bclinton' => array('firstName' => 'Bill', 'lastName' => 'Clinton', 'age' => 25, 'major' => 'Political Science', 'phone' => '933-440-3033', 'email' => '[email protected]', 'state' => 'AK'),); 

PHP

function display_person($query) { 
global $people; 
$foundid = array(); 
foreach ($people as $k => $v) 
if (stripos($k, $query) !== false) 
{ 
    $foundid[$k] = $v; 
} 
if(count($foundid) > 0) { 
    header('Content-type: application/json'); 
    echo json_encode($foundid); // NOTE: you need to change your JS code to accept array instead of 1 person 
} else { 
    header('HTTP/1.1 404 Not Found'); 
}} 

回答

1

的問題是,你的JSON的答案沒有一個適當的長度。嘗試添加一個

console.log(json.length); 

在您的JavaScript循環之前。

你可以找到答案了類似的問題在這裏:

get size of json object