【发布时间】:2014-05-12 06:24:09
【问题描述】:
所以基本上我的函数的返回值是一个二维数组,我打算显示每一行和每一列;但是,我在单独访问它们时遇到了麻烦,即 row[0][0] 我已经尝试了我在类似问题中看到的 stringify 函数,但没有让它工作。这是代码:
ajax:
$.ajax({
url: "viewDayDocuments",
type: 'post',
data: {currentDay: currentDay, currentMonth: currentMonth, currentYear: currentYear},
success: function(result){
$('.list').text('');
$('.list').remove();
incomingDoc = JSON.stringify(result);
$(".listIncoming").html("<p class = 'list'>This is the: "+ incomingDoc['referenceNo'] + "</p>");
$("#myform").show(500);
}
});
控制器代码:
$date = $data['year']."-".$data['month']."-".$data['day'];
$this->load->model('search_form');
$output['details'] = $this->search_form->searchDateRetrievedIncoming($date);
echo json_encode($output);
返回二维数组的模型代码:
$rows[] = array();
$rows2[] = array();
$rows3[] = array();
$i = 0;
$companyName = $this->db->query("SELECT id, name from company");
foreach($companyName->result_array() as $row2){
$rows2[$i]['id'] = $row2['id'];
$rows2[$i]['name'] = $row2['name'];
$i++;
}
//get all company names
$i = 0;
$staffName = $this->db->query("SELECT id, surname, firstName, middleInitial from employee");
foreach($staffName->result_array() as $row3){
$rows3[$i]['id'] = $row3['id'];
$rows3[$i]['name'] = $row3['surname'].", ".$row3['firstName']." ".$row3['middleInitial'];
$i++;
}
//get all employee names
$i= 0;
$output = $this->db->query("SELECT * from incoming WHERE dateReceived BETWEEN '$searchQuery' AND DATE(NOW())
ORDER BY incomingId LIMIT 20");
if ($output->num_rows() > 0) {
foreach($output->result_array() as $row){
$count = 0;
$j = 0;
$rows[$i]['incomingId'] = $row['incomingId'];
$rows[$i]['referenceNo'] = $row['referenceNo'];
$rows[$i]['documentTypeId'] = $row['documentTypeId'];
$rows[$i]['documentDate'] = $row['documentDate'];
$rows[$i]['dateReceived'] = $row['dateReceived'];
$rows[$i]['sender'] = $row['sender'];
while($count < sizeof($rows2)){
if($rows2[$j]['id'] != $row['companyId']){
$j++;
}else{
$rows[$i]['companyName'] = $rows2[$j]['name'];
break;
}
$count++;
}
$j= 0;
$count = 0;
while($count < sizeof($rows3)){
if($rows3[$j]['id'] != $row['responsibleStaffId']){
$j++;
}else{
$rows[$i]['responsibleStaffName'] = $rows3[$j]['name'];
break;
}
$count++;
}
$rows[$i]['subject'] = $row['subject'];
$rows[$i]['actionDone'] = $row['actionDone'];
$rows[$i]['track'] = $row['track'];
$rows[$i]['completed'] = $row['completed'];
$rows[$i]['remarks'] = $row['remarks'];
$i++;
}
return $rows;
}
return false;
工作答案:
incomingDoc = JSON.parse(result);
for(var i = 0; i < incomingDoc.details.length; i++){
$(".listIncoming").html("<p class = 'list'>This is the: "+ incomingDoc.details[i].referenceNo + "</p>");
}
【问题讨论】:
-
你能把你的 2D 数组贴出来看看吗?
-
基本上是 print_r($output) 我理解对了吗??
-
Array ( [0] => Array ( [incomingId] => 24 [referenceNo] => 908941 [documentTypeId] => 1 [documentDate] => 2014-05-06 [dateReceived] => 2014-05-12 [发件人] => 我 [companyName] => 新人民军 [responsibleStaffName] => Man, Alf B [subject] => testing [actionDone] => round [track] => 1 [完成] = > 0 [remarks] => round ) ) ...这是我的搜索功能的结果,无论如何它使用相同的功能
-
添加
console.log(result)和console.log(incomingDoc)以检查您收到的输出。您可以在 Firebug 中查看输出,以了解结果的结构。 -
我的数组是这样的:{"details":[{"incomingId":"24","referenceNo":"908941","documentTypeId":"1","documentDate":" 2014-05-06","dateReceived":"2014-05-12","sender":"me","companyName":"新人民军","responsibleStaffName":"Man, Alf B","subject ":"testing","actionDone":"round","track":"1","completed":"0","remarks":"round"}]}
标签: jquery ajax arrays codeigniter