I have no idea what I am doing past what I have here in my code already. In the below example I am attempting to have multiple cable types under each Cable Type column and row for each Lane Type, but as you can see only one cable type is being shown for each in the table.
I am trying to create a while loop or something, so that for each Lane Type it will produce as many cable types that are associated with said lane type, then move on to the next lane type doing the same. For lanes 5 and 6 I believe there is only one cable type for it anyway, so those look correct. How can I create a JSON array in PHP to group all the cable types together for each lane type, instead of it just putting every cable type into separate arrays apart from eachother?
What's happening now is even though you see multiple cable types per lane name or type, they are separated and I am wanting to group them together, so if I call array[0] that would be ALL of Lane-1's cable types, array1 would call all of Lane-2's cable types, etc etc. As of now, array[0] Just calls that first cable_type and not an array of cable types for that lane_name.
*EDIT (refer to my latest comment)*
Current PHP spits out following JSON array
{"Lane-1":["PPAHY4614","PPAHY464","HYBP464.01","PPA241807","ADMC1807"],"Lane-2":["HYBP906.1","PPAHY906","HP464.02","PP8HY461"],"Lane-3":["HYBP421","PAADY412"]}
OLD IMAGE:
*WORKING CURRENTLY*
PHP
//Create array
//Define JSON array
$array = array();
//Run SQL for each array value
$query="(SELECT DISTINCT lane_name,cable_type
FROM bundle_lanes
WHERE lane_name != '' AND lane_name != 'Shipped')
UNION
(SELECT DISTINCT lane_name,gp_cable_type
FROM bundle_lanes
WHERE lane_name != '' AND lane_name != 'Shipped')
ORDER BY lane_name";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
while ( $row = $result->fetch_assoc() ) {
$array[$row['lane_name']][] = $row['cable_type'];
}
echo json_encode($array);
Javascript
$.ajax({
url: './php/populate_cableTypes.php',
dataType:'json',
success: function (data) {
console.log(JSON.stringify(data));
//since data length is always 1,iterate through the 17 lane types
for (var n=1; n<=17; n++) {
//stringify data so it can be manipulated
var stringifyTypes = JSON.stringify(data["Lane-"+n]);
//check first if data is not null before populating
if (stringifyTypes != null) {
console.log("Lane-" + n + " string cable types: " + stringifyTypes);
//replace characters with new line breaks to format data
var cable_types = stringifyTypes.replace(/,/g, '<br />')
.replace(/\"/g, "")//backslash for quotes and brackets
.replace(/\[/g, "")
.replace(/\]/g, "");
//Show data in newly generated cable type td column for each lane type
$('td.Lane-'+n+'.weight').after("<td>" + cable_types + "</td>");
console.log("Lane-" + n + " formatted cable types: " + cable_types);
}
}
}
SELECT DISTINT lane_name, cable_type, gp_cable_type FROM bundle_lanes ORDER BY bundle ORDER BY lane_name ASC, cable_type ASC, gp_cable_type ASC
– Mike Brant Oct 21 '13 at 18:07$array[$row['lane_name']][] = $row['cable_type']
or$array[$row['lane_name']][] = $row['gp_cable_type']
depending on which field is set. – Mike Brant Oct 21 '13 at 18:29