2016-08-25 75 views
1

上午創建一個結果編譯器和我想要計算一個學生的總和,平均值和年級,但每當我嘗試計算總和結束給我零請幫助我..下面是我使用的代碼:如何使用php中的while循環計算總和和平均值

<?php 
require 'php/functions.php'; 

if (isset($_GET['student'])) { 
    $student = $_GET['student']; 
    $part  = explode(' ', $student); 
    $lastname = $part[0]; 
    $firstname = $part[1]; 




    //selecting values from database; 
    $query  = "SELECT * FROM `students` WHERE `lastname` = '$lastname' AND `firstname` = '$firstname'"; 
    $result  = mysql_query($query); 
    $numrows = mysql_num_rows($result); 

    $class  = mysql_result($result, 0, 'class'); 
    $house  = mysql_result($result, 0, 'house'); 
    $gender  = mysql_result($result, 0, 'gender'); 
    $ad_no  = mysql_result($result, 0, 'admission_no'); 



    if ($numrows == 1) { 
     ?> 
     <p>Lastname: <?php echo trim($lastname); ?></p> 
     <p>Firstname: <?php echo trim($firstname); ?></p> 
     <p>Class: <?php echo trim($class); ?></p> 
     <p>House: <?php echo trim($house); ?></p> 
     <p>Gender: <?php echo trim($gender); ?></p> 
     <p>Admission Number: <?php echo trim($ad_no); ?></p> 

     <table border="1" cellspacing="2" cellpadding="9"> 
    <tr> 
     <td>Subjects</td> 
     <td>C.A</td> 
     <td>M.T.E</td> 
     <td>Exam</td> 
     <td>Total</td> 
     <td>Grade</td> 
     <td>Remark</td> 

    </tr> 

    <tr> 
    <form method="POST" action=""> 
    <?php 
    $query  = "SELECT `name` FROM `senior_subject`"; 
    $result = mysql_query($query); 


     while ($rows = mysql_fetch_assoc($result)) { 
     echo "<td>".$rows['name']."<br/></td>"; 

     ?> 

    <td><input type="text" 
    name = "<?php echo $rows['name'].'_ca'; ?>" 
    placeholder = "<?php echo $rows['name']; ?> CA"/></td> 
    <td><input type="text" 
    name = "<?php echo $rows['name'].'_mte'; ?>" 
    placeholder = "<?php echo $rows['name']; ?> MTE"/></td> 
    <td><input type="text" 
    name = "<?php echo $rows['name'].'_exam'; ?>" 
    placeholder = "<?php echo $rows['name']; ?> EXAM"/></td> 
    <td><input type="text" disabled='disabled' name = "<?php echo $rows['name'].'_total'; ?>"/></td> 
    <td><input type="text" disabled='disabled' name = "<?php echo $rows['name'].'_grade'; ?>"/></td> 
    <td><input type="text" disabled='disabled' name = "<?php echo $rows['name'].'_remark'; ?>"/></td> 



    </tr> 
     <?php 
     if(isset($_POST['compile'])) { 
      $ca = $rows['name'].'_ca'; 
      $mte = $rows['name'].'_mte'; 
      $exam = $rows['name'].'_exam'; 
      $total = $rows['name'].'_total'; 
      $grade = $rows['name'].'_grade'; 
      $total = $rows['name'].'_remark'; 

      echo $ca + $mte + $exam; 

     } 
    } 
    ?> 
<input type="submit" value="Compile" name="compile"/> 

</table> 
</form> 

     <?php 

    } else { 
     echo "Student Does Not Exists"; 
    } 
} else { 
    echo "You don't have Permission to access this page"; 
} 
?> 
+0

輸入正確的代碼什麼是所有行中的$ rows ['name'] –

+0

您真的想在哪裏計算?和漂亮的SQL注入。 –

+1

每次有人使用不推薦的'mysql_ *'函數,一個可愛的小貓咪死在世界某個地方。 – roberto06

回答

0

試試這個

if(isset($_POST['compile'])) { 
     $ca = $rows['name'].'_ca'; 
     $mte = $rows['name'].'_mte'; 
     $exam = $rows['name'].'_exam'; 
     $total = $rows['name'].'_total'; 
     $grade = $rows['name'].'_grade'; 
     $total = $rows['name'].'_remark'; 
     $total += $total; 
} 

而while循環的開始在外面加總$ = 0;

In ending echo $ total;

+0

Tnx先生,但它顯示我空白後運行您的代碼而不是回聲的總數我不知道做了什麼錯誤 – Abdul

+0

您是否添加了while循環的總開始(外部) –

+0

是的,先生,我是否給了我零值 – Abdul