2016-08-25 6 views
1

Am結果を作るコンパイラと私は学生の合計、平均、等級を計算したいが、合計を計算しようとするとゼロが与えられる。以下のコードは、私が使用している: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

合計$ = 0を追加起動中whileループのこの

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; 
} 

と外部とを試してみてください。

終了時にecho $ total;

+0

Tnx Sirがコードを実行した後に空白を表示しましたが、何をしたのかわからない間違っている – Abdul

+0

whileループ(外)の合計$ beginを追加しました –

+0

はい私はそれが私にゼロの価値を与えてくれましたか – Abdul

関連する問題