对获取的数据求和

时间:2016-12-20 00:29:12

标签: php html sql

Goodmorning人!在这里我想做,我想总结两列,units_labunits_lec,但当我在下面运行我的代码变得混乱。这个问题的原因是什么?我的方法总结列是错误的吗?对此有什么正确的解决方法?

enter image description here

if(isset($_POST['loadsgrade'])){
    $stud = $_POST['stud'];
        $output = '';  
    $sql = "SELECT * FROM grades WHERE stud_no ='$stud'";
    $result = mysqli_query($con,$sql);
    $output .= '
                  <div class="table-responsive">
                    <table class="table table-bordered">
                      <tr>
                    <th>Subject Code</th>
                    <th>Section</th>
                    <th>Subject Decription</th>
                    <th>Units</th>
                    <th width="50">Prelim</th>
                    <th width="50">Midterm</th>
                    <th width="50">Finals</th>
                    <th width="100">Final Grade</th>
                      </tr>';

      while($row = mysqli_fetch_array($result))
      {
        $output .= '<tr>
                    <td>'.$row['subj_cd'].'</td>
                    <td>'.$row['section'].'</td>
                    <td>'.$row['subj_descr'].'</td>
                    <td>'.$row['units_lec'] + $row['units_lab'].'</td>
                    <td>'.$row['prelim_grade'].'</td>
                    <td>'.$row['midterm_grade'].'</td>
                    <td>'.$row['finals_grade'].'</td>
                    <td>'.$row['final_grade'].'</td>
                    </tr>
          ';
      }
          $output .= '</table>
            </div>';
   echo $output;


}

1 个答案:

答案 0 :(得分:2)

您应该将总和放在()中,否则PHP会对数字的+和字符串连接的.感到困惑。

<td>'. ($row['units_lec'] + $row['units_lab']) .'</td>