如何计算php中列的总和

时间:2017-04-17 00:10:39

标签: php

我想总结Course_Unit of courseoffer table和Course_Unit of education并将两者加在一起请帮助我,对于那些可怜的英语抱歉。

     <?php
  $sql="SELECT * FROM courseregistration where Matric = '1343128'";
 // $courseoffer=0;
 // $education=0;
  $qsql= mysqli_query($con,$sql);

while($rs1= mysqli_fetch_array($qsql))

 {
    //$id=$rs1['Course_id'];
    $sqlcourse_id1 ="SELECT sum(Course_Unit) As 'total' FROM courseoffer where ID='$id'";
    $qsqlcourse_id1 = mysqli_query($con,$sqlcourse_id1);
    $rscourse_id1 = mysqli_fetch_assoc($qsqlcourse_id1);

 $sqlcourse_id ="SELECT sum(Course_Unit) AS 'totalx' FROM education where ID='$rs1[Course_id]'";
    $qsqlcourse_id = mysqli_query($con,$sqlcourse_id);
    $rscourse_id = mysqli_fetch_assoc($qsqlcourse_id);

    $courseoffer=$rscourse_id1['total'];
   $education=$rscourse_id['totalx'];

    $answer=$courseoffer;//+$education;
   //$courseoffer+=$rscourse_id1['Course_Unit'];
 //$education+=$rscourse_id['Course_Unit'];

 }

    $answer=$courseoffer+$education;
     ?>

  <tr>
    <td>&nbsp;<?php echo "$answer" ?></td>


     </tr>


 ?>

https://i.stack.imgur.com/VC4GL.png

1 个答案:

答案 0 :(得分:0)

<强>已更新

$ sum给出行总数,$ allsum值给出累计值

 <?php
  $sql="SELECT * FROM courseregistration where Matric = '1343128'";
 // $courseoffer=0;
 // $education=0;
  $qsql= mysqli_query($con,$sql);

$allsum=0;

while($rs1= mysqli_fetch_array($qsql))

 {
//$id=$rs1['Course_id'];
$sqlcourse_id1 ="SELECT sum(Course_Unit) As 'total' FROM courseoffer where ID='$id'";
$qsqlcourse_id1 = mysqli_query($con,$sqlcourse_id1);
$rscourse_id1 = mysqli_fetch_assoc($qsqlcourse_id1);

 $sqlcourse_id ="SELECT sum(Course_Unit) AS 'totalx' FROM education where ID='$rs1[Course_id]'";
$qsqlcourse_id = mysqli_query($con,$sqlcourse_id);
$rscourse_id = mysqli_fetch_assoc($qsqlcourse_id);

$sum = $rscourse_id1['total'] + $rscourse_id['totalx'];
$allsum+=$sum;

 }

 ?>

  <tr>
  <td>&nbsp;<?php echo $allsum; ?></td>
   </tr>