好的。你需要的代碼如下。我測試了它,它工作。
說明:它爲表中存在的每個日期和cse匹配一個div,其中包括presentiew的nuber和absenties的數量。然後,使用javascript,當選擇選擇時,它會檢查是否選擇日期和cse選擇。如果是的話,它會使div與選定的日期和cse可見。如果沒有相應的div(因爲該數組在當天沒有該學生),則顯示相應的消息。否則顯示所選日期和cse的數據。
它的功能還包括在評論中。如果你有任何問題或者你想要不同的東西請讓我知道。
<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>
你想打印的數據,你選擇後立即從下拉列表和日期選擇器的選項,或者你想有一個按鈕,當用戶點擊該按鈕刷新頁面,你需要的數據(學生presntees和absenties)將被打印? – Thanasis
我想要數據,當日期和部分選擇日期選擇器和DropDown – user7399549