I have an ajax post that turns the JSON that looks like this
{"0":{"Project_ID":"1","Project_Name":"TEST 1"...},
"1":{"Project_ID":"1","Project_Name":"TEST...etc.`
into an object that can be accessed by using result[0]['Whatever Key']
or result[1]['Whatever Key']
. Most of the data is the same considering my query grabs information for just 1 project, but the reason I have multiple results is I have data that fits into more than one location, or cost center category, etc.
I'm unsure how to iterate through the number of results and use that number in a for loop to populate a table with all the values instead of just the ones in index [0]
.
The script looks like this:
echo "<script type'text/javascript'>
$('#result_year').change(function(event) {
$.post('info.php', { selected_project: $('#selection_project option:selected').val(), selected_year: document.getElementById('result_year').value},
function(data) {
var result = JSON.parse(data);
//maybe an $.each loop here? or
//for(var i=0; i<result.length; i++) <--(but NOT the length, the number of rows?)
var location = result['Location'];
var cccName = result['Cost_Center_Category_Name'];
var ccName = result['Cost_Center_Name'];
var Q_1 = result['Q_1'];
var Q_2 = result['Q_2'];
var Q_3 = result['Q_3'];
var Q_4 = result['Q_4'];
$('.editable').empty();
document.getElementById('MQ'+location+cccName+ccName+'Q_1').innerHTML=Q_1;
document.getElementById('MQ'+location+cccName+ccName+'Q_2').innerHTML=Q_2;
document.getElementById('MQ'+location+cccName+ccName+'Q_3').innerHTML=Q_3;
document.getElementById('MQ'+location+cccName+ccName+'Q_4').innerHTML=Q_4;
}
);
});
</script>;";
Here's my info.php as well
<?php
require ('system.php');
if (isset($_POST['selected_project']))
$selected_project = $_POST['selected_project'];
if (isset($_POST['selected_year']))
$selected_year = $_POST['selected_year'];
$query = "SELECT Project_Info.Project_ID, Project_Name, Global_Project_Number, Project_Type.Project_Type_Name, Lead_Location, Local_Project_Number_1, Local_Project_Number_2, Local_Project_Number_3, Local_Project_Number_4, Local_Project_Number_5, Development_Location_2, Development_Location_3, Development_Location_4, Development_Location_5, Customer, Duration, Customer_Group, Average_Number_of_Pieces, State, Business_Center.BC_Name, Start_Of_Production, Outlets.Outlet_Name, End_Of_Development, Start_Of_Development, Project_Connection, Project_Manager, Chance_Budget, System_TPL, Chance_Forecast, Product_Family, Technology, LL_SW, Processor_Type, HL_SW, Chassis_Type, Technology_Development, Technical_Description, username, Milestones.Description AS Milestone_Description, Milestone_1, Milestone_2, Milestone_3, Milestone_4, Milestone_5, Milestone_6, Due_Date_1, Due_Date_2, Due_Date_3, Due_Date_4, Due_Date_5, Due_Date_6, Labour_Planning.*, Cost_Center_Category.Cost_Center_Category_Name, Cost_Centers.Cost_Center_Name
FROM Project_Info
LEFT JOIN Project_Type ON Project_Type.Project_Type_ID = Project_Info.Project_Type
LEFT JOIN Business_Center ON Business_Center.BC_ID = Project_Info.BU_CC
LEFT JOIN Outlets ON Outlets.Outlet_ID = Project_Info.Outlet
LEFT JOIN Milestones ON Milestones.Project_ID = Project_Info.Project_ID
LEFT JOIN Labour_Planning ON Labour_Planning.Project_ID = Project_Info.Project_ID
AND Labour_Planning.Year = $selected_year
LEFT JOIN Cost_Center_Category ON Cost_Center_Category.Cost_Center_Category_ID = Labour_Planning.Cost_Center_Category
LEFT JOIN Cost_Centers ON Cost_Centers.Cost_Center_Number = Labour_Planning.Cost_Center_Number
WHERE Project_Info.Project_ID = '$selected_project';";
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()){
$results[] = $row;
}
$results['selected_year'] = $selected_year;
$results['selected_project'] = $selected_project;
$response = json_encode($results);
echo $response;
?>
Any help would be very much appreciated!