我有一个jsfiddle here,我用它来使我的概念有效。
我可能遇到的问题是由JSON引起的,我会分享我用来生成它的代码。
首先我做一个查询:
$sth->execute();
$last_sup = 0;
$i = 0;
$reorderArr = array();
$prodsArr = array();
$si = 0;
while ($row = $sth->fetchObject()) {
// If new supplier.
if ($last_sup !== $row->sup_id) {
$last_sup = $row->sup_id;
// If not first iteration.
if ($i !== 0) {
// Append $prodsArr to current $supArr
$supArr['prods'] = (array)$prodsArr;
// empty $prodsArr ready for next sup.
$prodsArr = array();
// Append $supArr to previous $reorderArr
$reorderArr[$si] = (array)$supArr;
$si++;
}
// Create entries for new supplier to $supArr
$supArr = array(
"supID" => $row->sup_id,
"supName" => $row->supplier_comp_name
);
}
// Build array of products for this supplier.
$prodsArr[] = array(
"pID" => $row->prod_id,
"sku" => $row->sku,
"pName" => $row->prod_name,
"cat" => $row->category_name,
"desc" => $row->prod_desc,
"stock_level" => $row->stock_level,
"reOrdLev" => $row->reorder_level,
"reOrdQty" => $row->reorder_qty,
"vat_exempt" => $row->vat_exempt,
"lastorderdate" => $row->lastorderdate,
"lastorderqty" => $row->lastorderqty,
"qty_in_outer" => $row->qty_in_outer,
"cost_per_outer" => $row->cost_per_outer
);
$i++;
}
// Process the very last loop (since it's normally processed
// at the start of the next loop.
// Append $prodsArr to current $supArr
$supArr[] = (array)$prodsArr;
// Append $supArr to previous $reorderArr
$reorderArr[$si] = (array)$supArr;
return $reorderArr;
然后我加倍 json_encode
它(因为我接下来的教程告诉我)并用Jquery解析它:
var data = $.parseJSON(<? print json_encode(json_encode($reorderArr));?>);
我在页面上的脚本标签之间加入了上面一行,并将其发送到我的函数,以便使用以下代码进行处理:
displayReorderList(data);
以下是displayReorderList()
函数的当前状态:
function displayReorderList($data) {
var table = document.createElement('table');
/**
* Set up supplier group.
* 1 loop per group.
*/
for(var i=0; i < data.length; i++) {
var tr = document.createElement('tr');
var td1 = document.createElement('td');
// Need to put the Supplier name here in a colspan cell.
var text1 = document.createTextNode(data[i].supName);
td1.appendChild(text1);
tr.appendChild(td1);
table.appendChild(tr);
/**
* Setup Product Group.
* 1 loop per product.
*/
for(var j = 0; j < data[i].prods.length; j++) {
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var text1 = document.createTextNode(data[i].prods[j].sku);
td1.appendChild(text1);
tr.appendChild(td1);
table.appendChild(tr);
}
}
document.body.appendChild(table);
为了简单概述数据结构,这里有一个print_r($reorderArr)
Array
(
[0] => Array
(
[supID] => 2
[supName] => Sparks
[prods] => Array
(
[0] => Array
(
[pID] => 7
[sku] => 7
[pName] => Term Block
[cat] => Electrical
[desc] => Nylon connector block.
[stock_level] => 3
[reOrdLev] => 5
[reOrdQty] => 20
[vat_exempt] => 0
[lastorderdate] =>
[lastorderqty] =>
[qty_in_outer] => 1
[cost_per_outer] => 60.00
)
[1] => Array
(
[pID] => 5
[sku] => 5
[pName] => Electrical Tape
[cat] => Electrical
[desc] => Black
[stock_level] => 12
[reOrdLev] => 20
[reOrdQty] => 100
[vat_exempt] => 0
[lastorderdate] =>
[lastorderqty] =>
[qty_in_outer] => 1
[cost_per_outer] => 39.00
)
)
)
[1] => Array
(
[supID] => 9
[supName] => Prime Plumbing Inc.
[prods] => Array
(
[0] => Array
(
[pID] => 6
[sku] => 6
[pName] => BlowGas
[cat] => Plumbing
[desc] => 400g Canister
[stock_level] => 6
[reOrdLev] => 15
[reOrdQty] => 60
[vat_exempt] => 0
[lastorderdate] =>
[lastorderqty] =>
[qty_in_outer] => 1
[cost_per_outer] => 142.00
)
)
)
[2] => Array
(
[supID] => 12
[supName] => Trade Plumbing Supplies Inc.
[0] => Array
(
[0] => Array
(
[pID] => 1
[sku] => 1
[pName] => PTFE Tape
[cat] => Plumbing
[desc] => 10mm x 3m
[stock_level] => 9
[reOrdLev] => 10
[reOrdQty] => 50
[vat_exempt] => 0
[lastorderdate] =>
[lastorderqty] =>
[qty_in_outer] => 1
[cost_per_outer] => 24
)
[1] => Array
(
[pID] => 14
[sku] => 14
[pName] => Antique Tap Set
[cat] => Plumbing
[desc] => Gold/Ceramic bathroom set.
[stock_level] => 2
[reOrdLev] => 2
[reOrdQty] => 3
[vat_exempt] => 0
[lastorderdate] =>
[lastorderqty] =>
[qty_in_outer] => 1
[cost_per_outer] => 2800.00
)
)
)
)
我正在尝试最终得到已达到其重新订购级别的产品列表,按供应商分组,但脚本似乎停留在for(var j = 0; j < data[i].prods.length; j++) {
,但仅在经过多次迭代后才停止。似乎data[i].prods
的长度被错误报告,可能是因为JSON格式错误了?
请参阅jsfiddle。
更新
在@Yogesh指出我正确的方向后,我更新如下。
我已在服务器端阵列构造中将$supArr[] = (array)$prodsArr;
更改为$supArr[prods] = (array)$prodsArr;
。
这样做之后,一切都很顺利。请使用正确生成的数据查看jsfiddle here。
答案 0 :(得分:2)
你的数组的第三个元素不包含prods
数组,请参见此处
[2] => Array
(
[supID] => 12
[supName] => Trade Plumbing Supplies Inc.
[0] => Array
^// its 0 not prods
[1] => Array
(
[supID] => 9
[supName] => Prime Plumbing Inc.
[prods] => Array
^// its prods
因此,数据[i] .prods的长度报告错误。
此处有if
条件,因此prods
并非适用于所有数组
if ($i !== 0) {
// Append $prodsArr to current $supArr
$supArr['prods'] = (array)$prodsArr;