2017-02-07 78 views
0

我有一個excel表單,鏈接如下。 在這張表中,我給了3個cse部分。 https://i.stack.imgur.com/aHidC.png 我想在php中顯示它們,當我從日期選擇器中選擇日期並從下拉菜單中選擇該節時,應顯示缺席者。我不知道該怎麼做。如何根據從datepicker中選擇的日期獲取不同的數據?

$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. 
    "\n"; 
    echo "Present Totals: ".$value['present']. 
    "\n"; 
    echo "absent Totals: ".$value['absent']. 
    "\n"; 
    echo "--------------\n"; 
} 

我試過了。但我很困惑,從datepicker的日期和下拉的部分選擇如何顯示該日期學生的存在和缺席選定的選項。

+0

你想打印的數據,你選擇後立即從下拉列表和日期選擇器的選項,或者你想有一個按鈕,當用戶點擊該按鈕刷新頁面,你需要的數據(學生presntees和absenties)將被打印? – Thanasis

+0

我想要數據,當日期和部分選擇日期選擇器和DropDown – user7399549

回答

0

好的。你需要的代碼如下。我測試了它,它工作。

說明:它爲表中存在的每個日期和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> 
相關問題