2015-11-06 124 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);