php - 无法通过从 php 文件传递 JSON 数据来使用 jquery 数据表填充数据表
问题描述
我正在尝试使用来自 Jquery 的数据表插件并从 PHP 传递 JSON 数据来填充数据表。但是我收到无效的 JSON 错误。我已经使用 JSON lint 验证了我的 json,它成功验证了它
我的代码如下所示:
//SQL query to get data from Database
$sql_fabentry="select f.fabrication_date as date,concat(w.workstage_name,':',f.fabricator_remarks)"
. " as workstage_entry,u.user_name from fabrication_record f,workstage w,"
. "users u where f.workstage_id=w.workstage_id and u.user_id=f.fabricator_id "
. "and f.subsystem_id='$subsystem_id_session' and f.cp_no IS NULL";
$sql_qcentry="select f.qc_date as date,concat(r.remark,':',f.qc_remarks)"
. " as workstage_entry,u.user_name from fabrication_record f,remarks r,"
. "users u where f.qcremarks_id=r.remark_id and u.user_id=f.qcinspector_id "
. "and f.subsystem_id='$subsystem_id_session' and f.cp_no IS NULL";
//db_select function return an associative array
//result from both queries will always return same number of rows
$results_fabentry=$db->db_select($sql_fabentry);
$results_qcentry=$db->db_select($sql_qcentry);
//Here iam trying to combine the two arrays in one array with alternating
//rows from each array
$count1=count($results_fabentry);
$i=0;
$j=0;
$result=array();
while($i<$count1){
$result[$j]['date']=$results_fabentry[$i]['date'];
$result[$j]['workstage_entry']=$results_fabentry[$i]['workstage_entry'];
$result[$j]['user_name']=$results_fabentry[$i]['user_name'];
$result[$j+1]['date']=$results_qcentry[$i]['date'];
$result[$j+1]['workstage_entry']=$results_qcentry[$i]['workstage_entry'];
$result[$j+1]['user_name']=$results_qcentry[$i]['user_name'];
$i++;
$j+=2;
}
$results=["sEcho=>1",
"iTotalRecords"=>count($result),
"iTotalDisplayRecords"=>($result),
"aaData"=> $result];
echo json_encode($results);
我已经验证了 $ result 中的 JSON 数据并且它是有效的。如果删除 while 循环并尝试遵循它的工作原理:
$results=["sEcho=>1",
"iTotalRecords"=>count($results_fabentry),
"iTotalDisplayRecords"=>($results_fabentry),
"aaData"=> $results_fabentry];
echo json_encode($results);
谁能解释我做错了什么?
解决方案
我能够通过用 foreach 循环替换 while 循环来解决问题。我使用了以下代码并且工作正常
$result = [];
$i=0;
$j=1;
foreach($results_fabentry as $result_fabentry){
$result[$i]['date']=$result_fabentry['date'];
$result[$i]['workstage_entry']=$result_fabentry['workstage_entry'];
$result[$i]['user_name']=$result_fabentry['user_name'];
$result[$i+1]['date']="0";
$result[$i+1]['workstage_entry']="0";
$result[$i+1]['user_name']="0";
$i+=2;
}
foreach($results_qcentry as $result_qcentry){
$result[$j]['date']=$result_qcentry['date'];
$result[$j]['workstage_entry']=$result_qcentry['workstage_entry'];
$result[$j]['user_name']=$result_qcentry['user_name'];
$j+=2;
}
$results=["sEcho=>1",
"iTotalRecords"=>count($result),
"iTotalDisplayRecords"=>($result),
"aaData"=> $result];
echo json_encode($results);
推荐阅读
- autodesk-forge - Autodesk Forge 将 DWG 上的许多布局转换为 PDF
- objective-c - NSSound 停止功能
- node.js - 如何根据 MongoDB 数据库中的键获取多个结果?
- python-3.x - 如何克服“盐必须是字节字符串错误”?
- python - nltk 和下载的 Chaquopy 问题
- node.js - 是否可以更新 IP2Location 数据库?
- javascript - 如何按数组对项目进行排序?
- sql - 在 SQL 中查询循环和随机
- snowflake-cloud-data-platform - Snowpipe rest api 总是返回“无效的 jwt 令牌”
- reactjs - React 中的状态无缘无故未定义?