I have an excel sheet which link is given below. In this sheet I gave 3 sections of cse. I want to display them in php and when i select the date from date picker and section from drop down that section presented and absentees should be display. I don't know how to do this.
$students = [
['CSE-A' => 'PRESENT'],
['CSE-A' => 'PRESENT'],
['CSE-B' => 'ABSENT'],
['CSE-B' => 'ABSENT'],
['CSE-A' => 'ABSENT'],
['CSE-B' => 'PRESENT'],
['CSE-B' => 'ABSENT'],
['CSE-A' => 'ABSENT'],
['CSE-C' => 'ABSENT'],
['CSE-C' => 'PRESENT']
];
$outputs = [];
foreach($students as $array) {
foreach($array as $key => $value) {
if (!isset($outputs[$key])) {
$outputs[$key] = [
'present' => 0,
'absent' => 0,
];
}
//check value
if ($array[$key] === 'PRESENT') {
$outputs[$key]['present']++;
} else if ($array[$key] === 'ABSENT') {
$outputs[$key]['absent']++;
}
}
}
foreach($outputs as $key => $value) {
echo $key.
"
";
echo "Present Totals: ".$value['present'].
"
";
echo "absent Totals: ".$value['absent'].
"
";
echo "--------------
";
}
I tried up to this. But I am confusing when date from datepicker and section from dropdown selected how to display that date students presences and absences on selected option.
Ok. The code you need is below. I tested it and it works.
Explanation: It crates a div for every match of dates and cse that exists in the table, which includes the nuber of presentiew and the number of absenties. Then, using javascript, when a selection is choosed, it checks if both the date and the cse selection is chosen. If yes, it makes the div with the selected date and cse visible. If there isn't the corresponding div (because the array doesn't have a student for that cse in that day) an appropriate message is show. Otherwise the data for the selected date and cse are shown.
What it does is also included in the comments. If you have any question or you wanted something different please let me know.
<html>
<?php
$students = array
(
array('CSE-A','PRESENT',"2-10-2017"),
array('CSE-A','PRESENT',"2-10-2017"),
array('CSE-B','ABSENT',"3-10-2017"),
array('CSE-B','ABSENT',"3-10-2017"),
array('CSE-A','ABSENT',"3-10-2017"),
array('CSE-B','PRESENT',"4-10-2017"),
array('CSE-B','ABSENT',"4-10-2017"),
array('CSE-A','ABSENT',"5-10-2017"),
array('CSE-C','ABSENT',"5-10-2017"),
array('CSE-C','PRESENT',"5-10-2017")
);
$outputs = [];
foreach($students as $array) {
$cse = $array[0];
$present = $array[1];
$date = $array[2];
$entry = $cse.", ".$date;
if (!isset($outputs[$entry])) {
$outputs[$entry] = [
'present' => 0,
'absent' => 0,
];
}
//check value
if ($present === 'PRESENT') {
$outputs[$entry]['present']++;
} else if ($present === 'ABSENT') {
$outputs[$entry]['absent']++;
}
}
// Save all availbale cse, for the dropdown choices
$all_cse = array();
foreach($students as $array) {
$cse = $array[0];
array_push($all_cse,$cse);
}
$all_cse = array_combine($all_cse, $all_cse);
// Save all availbale dates, for the dropdown choices
$all_dates = array();
foreach($students as $array) {
$date = $array[2];
array_push($all_dates,$date);
}
$all_dates = array_combine($all_dates, $all_dates);
?>
<body>
<select id="cse" onchange="myFunction()">
<option disabled selected value> -- select an option -- </option>
<?php
foreach($all_cse as $cse) {
echo "<option value='".$cse."'>".$cse."</option>";
}
?>
</select>
<select id="dates" onchange="myFunction()">
<option disabled selected value> -- select an option -- </option>
<?php
foreach($all_dates as $date) {
echo "<option value='".$date."'>".$date."</option>";
}
?>
</select>
<br><br>
<div id="data">
</div>
<?php
foreach($outputs as $key => $value) {
// Create a div for every match of cse and date that exists.
echo "<div id='".$key."' style='display:none;'>";
"<br>";
echo "Present Totals: ".$value['present'].
"<br>";
echo "absent Totals: ".$value['absent'].
"<br>";
echo "</div>";
}
?>
</body>
</html>
<script type="text/javascript">
var lastDiv;
function myFunction() {
var date = document.getElementById("dates").value;
var cse = document.getElementById("cse").value;
if (cse!="" && date!=""){
// Make invisible the div for the previous choice
if (lastDiv!=null){
lastDiv.style.display = "none";
}
lastDiv = document.getElementById(cse + ", " + date);
// Make visible the div for the current choice
if (lastDiv!=null){
document.getElementById("data").innerHTML = cse +"<br>Date: " + date;
lastDiv.style.display = "block";
} else {
document.getElementById("data").innerHTML = "No matches found for this cse in this date.";
}
}
}
</script>