I am trying to combine parameters from PHP and JS. I looked thru internet and went thru the answers of simular questions in this forum. But still I did not get an elegant solution.
The "(de)select all checkboxes" function from JS selected all checkboxes from the page and not the ones belonging to a single groupname. To solve that I used different ID='' for each groupname.
Dont get me wrong. But the code below WORKS! And it does what it has to do, but it is, by my opinion, not the best way to do it. Has anyone a more elegant, and maybe faster, solution?
The PHP code:
echo("<div id='MainMenu'><div class='list-group panel'>");
$iRow = 1;
foreach($aAllClasses as $aClassName){
$sGroupname = str_replace('.', '_', $aClassName[0]);
echo("<a href='#class".$iRow."' class='list-group-item list-group-item-success' data-toggle='collapse' data-parent='#MainMenu'>".$aClassName[0]."<i class='fa fa-caret-down'></i></a>
<div class='collapse' id='class".$iRow."'>");
$aClassStudents = $this->fetchClassStudents($aClassName[0]);
echo("<input type='checkbox' id='checkUncheckAll".$sGroupname."' onClick='CheckUncheckAll".$sGroupname."()'><b> De gehele groep.</b><br>");
foreach($aClassStudents as $aStudentRecord){
echo("<input type='checkbox' class='selectable".$sGroupname."' value='T' name='student".$aStudentRecord['id']."'> ".$aStudentRecord['sStudentSurname'].", ".$aStudentRecord['sStudentFirstname']." ".$aStudentRecord['sStudentInsertion']." (".$aStudentRecord['iStudentNumber'].")<br>");
}
echo("</div>");
$iRow++;
}
echo("</div></div>");
The JS script looks horrible:
<?php
$aAllClasses = $oDBactions->fetchAllClasses($eUserIP);
?>
<script>
<?php
foreach($aAllClasses as $aClassName){
$sGroupname = str_replace('.', '_', $aClassName[0]);
?>
function CheckUncheckAll<?php echo($sGroupname); ?>(){
var selectAllCheckbox=document.getElementById("checkUncheckAll<?php echo($sGroupname); ?>");
if(selectAllCheckbox.checked==true){
var checkboxes = document.getElementsByClassName("selectable<?php echo($sGroupname); ?>");
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = true;
}
}else {
var checkboxes = document.getElementsByClassName("selectable<?php echo($sGroupname); ?>");
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = false;
}
}
}
<?php } ?>
</script>