I have a jsfiddle here, which I'm using to get my concept working.
It's likely that the issues I'm having are caused by the JSON, I'll share the code I use to generate that.
First I do a query:
$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;
Then I double json_encode it (because the tutorial I followed told me to) and parse it with Jquery like so:
var data = $.parseJSON(<? print json_encode(json_encode($reorderArr));?>);
and I include the line above between script tags on my page and send it to my function for processing it using the line below:
displayReorderList(data);
Here is the displayReorderList() function in it's current state:
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);
To give an easy overview of the data structure, here is a 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
)
)
)
)
I'm attempting to end up with a lists of products which have reached their reorder levels, grouped by supplier but the the script seems to be stalling at for(var j = 0; j < data[i].prods.length; j++) { but only after iterating past that line several times. It seems like the length of data[i].prods is being incorrectly reported, maybe because the JSON is malformed?
Please see the jsfiddle.
UPDATE
I've updated as follows, after @Yogesh pointed me in the right direction.
I have changed $supArr[] = (array)$prodsArr; to $supArr[prods] = (array)$prodsArr; in the server-side array construction.
After doing that, it all worked nicely. See jsfiddle here with the correctly generated data.