2015-11-06 131 views
1

我想對每個學生的學生出勤率百分比進行計算,並且會顯示屬於每個學生的每個出勤率百分比。下面是我的代碼:在php中計算mysql mysql

<tbody> 
    <?php 
     $data = "SELECT SUM(studAtt_endTime - studAtt_startTime) FROM studentAttendance"; 
     $result = $conn->query($data) 
     or die ("Error: ".mysqli_error($conn)); //look if theres any error 
      while($ser2=mysqli_fetch_array($result)) { 
    ?> 

    <?php 
     $counter = 1; 
     $data = "SELECT * FROM student INNER JOIN studentAttendance ON student.stud_matric=studentAttendance.student_stud_matric 
          INNER JOIN course ON course.course_code=studentAttendance.course_course_code 
          WHERE course_course_code LIKE '%$_GET[course]%' 
          GROUP BY student_stud_matric"; 
       $result = $conn->query($data) 
       or die ("Error: ".mysqli_error($conn)); //look if theres any error  
        while($ser=mysqli_fetch_array($result)) { 
    ?> 
       <tr> 
       <td><center><?php echo $counter; 
          $counter++; ?></center></td> 
       <td><?php echo $ser["stud_matric"];?></td> 
       <td><?php echo $ser["stud_name"];?></td> 
       <td><?php 
         $a = $ser["course_contacthour"]; 
         $b = "14"; 
         $tch = ($a * 2) * $b; // tch= total contact hour in 14 week 
         echo number_format($ser2["SUM(studAtt_endTime - studAtt_startTime)"]/$tch * 100, 2);?></td> 
       </tr> 
            <?php 
            } 
            ?> 
            <?php 
            } 
            ?> 
           </tbody> 

的問題是,這個代碼將總結所有的學生出席率,它會顯示每個學生相同的百分比,而不是對學生本身的百分比。

+1

你有一個使用'$ result'的嵌套sl查詢 - 這也會被外部查詢使用,這可能會導致問題。 – RamRaider

+0

還有其他問題。爲什麼兩個查詢? YOu錯過了第二個查詢中參數名稱的引號(即:''%$ _ GET [course]%''應該是''%{$ _ GET ['course']}%'','number_format($ ser2 [「SUM(studAtt_endTime - studAtt_startTime)」]/$ tch * 100,2)'?? – RamRaider

+0

我有兩個查詢,因爲它不能通過一個查詢來完成,或者它不能寫出正確的查詢 'number_format $ ser2 [「SUM(studAtt_endTime - studAtt_startTime)」]/$ tch * 100,2)'是學生考勤百分比的計算 因此,我需要第一個查詢中的SUM(studAtt_endTime - studAtt_startTime)' ,所顯示的結果是從所有學生的總和'SUM(studAtt_endTime - studAtt_startTime)'計算出來的,而不是像我想要的每個特定學生。@RamRaider –

回答

1
<html> 
    <head> 
     <title></title> 
    </head> 
    <body> 
     <table> 
      <tbody> 
       <?php 

       $counter = 1; 

       /* Some rudimentary filtering at the very least, better use prepared statements or PDO */ 
       $course = addslashes(filter_input(INPUT_GET,'course',FILTER_SANITIZE_STRING)); 

       /* I think you can combine the two sql queries and have only 1 loop */ 
       $data = "select *, 
          (select sum(`studatt_endtime` - `studatt_starttime`) from `studentattendance`) as 'sum' 
          from `student` s 
          inner join `studentattendance` a on s.`stud_matric`=a.`student_stud_matric` 
          inner join `course` c on c.`course_code`=a.`course_course_code` 
          where a.`course_course_code` like '%".$course."%' 
          group by a.`student_stud_matric`;"; 

       /* Query the db - do not reveal too much information if there is a problem */ 
       $result = $conn->query($data) or die ("Error: Something went wrong..."); 
       if($result){ 

        /* Loop through recordset */ 
        while($ser=$result->fetch_object()) { 

         /* Do your calculations */ 
         $a = $ser->course_contacthour; 
         $b = "14"; 
         $tch = ($a * 2) * $b; 
         $tot=number_format($ser->sum/$tch * 100, 2); 

         /* write the output */ 
         echo ' 
         <tr> 
          <td> 
           <center>'.$counter.'</center> 
          </td> 
          <td>'.$ser->stud_matric.'</td> 
          <td>'.$ser->stud_name.'</td> 
          <td>'.$tot.'</td> 
         </tr>'; 

         $counter++; 
        } 
       } 
       @mysqli_free_result($result); 
      ?> 
      </tbody> 
     </table> 
    </body> 
</html> 
+0

是的,這段代碼我更加理解tq這麼多!但是我仍然不能得到計算的權利。看看我在編輯過的@RamRaider上發表的文章 –

0

而不是編輯上面/下面和混淆的東西,我會在這裏發佈一些其他的東西。沒有看到的模式,我不知道,如果下面的SQL是正確的,我不知道如果列studatt_endtimestudatt_starttime都在studentAttendance表或student表,所以你可能需要更改使用的前綴:

/* Now the `sum` is pertinent to the student whereas before it was not */ 
$data = "select *, 
     sum(a.`studatt_endtime` - a.`studatt_starttime`) as 'sum' 
     from `student` s 
     inner join `studentattendance` a on s.`stud_matric`=a.`student_stud_matric` 
     inner join `course` c on c.`course_code`=a.`course_course_code` 
     where a.`course_course_code` like '%".$course."%' 
     group by a.`student_stud_matric`;"; 

另外,我不知道你在計算什麼,數學對我來說從來就不是一個強項,但是計算似乎有點含糊,因爲對BODMAS計算方法沒有任何要求,其中BODMAS代表Brackets, Order, Division, Multiplication, Addition, Subtraction - see here for details所以計算可能以不同的方式interprested:

$tot=number_format($ser->sum/($tch * 100), 2); 
$alt_tot=number_format(($ser->sum/$tch) * 100, 2);