Maybe I am doing this a very long way around by converting a php array $exams to a JS object examType only to use the elements of that object and try to convert them back to php.
I think I should try to put the JS inside the php.
Perhaps it should work this way,
var exam = this.value;
var resultArray = [];
<?php foreach($exams as $element): ?>
if(exam == <?php $element ?>){
resultArray = <?php echo json_encode($subs[$element], JSON_PRETTY_PRINT); ?>;
}
<?php endforeach; ?>
});
I tried improving the function using json_encode() but code doesn’t seem to be able to compare the two variables.
function examTables(){
var exam = this.value;
console.log(exam);
var resultArray = [];
<?php foreach($exams as $element): ?>
var other = <?php echo json_encode($element); ?>;
if(exam == other){
resultArray = <?php echo json_encode($subs[$element], JSON_PRETTY_PRINT); ?>;
}else{
resultArray = <?php echo json_encode($subs['AP'], JSON_PRETTY_PRINT); ?>;
}
<?php endforeach; ?>
var strOption = "";
strOption = "<table>";
var keys1 = Object.keys(resultArray);
keys1.forEach(function(item1){
var keys2 = Object.keys(resultArray[item1]);
keys2.forEach(function(items2){
var keys3 = Object.keys(resultArray[item1][items2]);
value = resultArray[item1][items2];/*Need to not repeat subjects. This should be done in php file when selecting from DB*/
strOption += "<tr><td>" + value + "</td></tr>";
});
});
strOption += "</table>";
var table = document.getElementById("examTables");
var row = document.createElement("tr");
var cell = document.createElement("td");
cell.innerHTML = strOption;
row.appendChild(cell);
table.appendChild(row);
}