0

Any one help me please am new in php, I real need help.

I have this php script which output student exam records, but one thing remain is sum of total marks, I fail how to display Total at the end near "Average".

    <?php
    $fname="fname";
    $exam_name="exam_name";
    $date="date";
    $class="class";
    $query="query";
    $query2="query2";
    $database=("mcl");
    mysql_connect("localhost","root","mcl");
    @mysql_select_db(mcl) or die( "Unable to select database");


if(isset($_REQUEST['submit'])){
$fname=$_POST['fname'];
$date=$_POST['date'];
   $exam_name=$_POST['exam_name'];
$class=$_POST['class'];

   $sql=" SELECT * FROM stdreg_exam WHERE fname like '%".$fname."%' AND exam_name    
    like   
    '%".$exam_name."%'AND class like '%".$class."%' AND date like '%".$date."%'";

 $sql2 ="SELECT stdreg_exam.*, 
   (mathematics+english+kiswahili+geograph+civics+french+religion+pds+vskill+
   ict+science+history)/12 as Average FROM stdreg_exam 
         WHERE fname like '%".$fname."%' AND exam_name like '%".$exam_name."%'AND   
   class like '%".$class."%' AND date like '%".$date."%'";


   $q=mysql_query($sql);
$q=mysql_query($sql2);

  }
   else{
  $sql="SELECT * FROM stdreg_exam";
  $q=mysql_query($sql);
   }
  ?>
 <form method="post">
 <table width="500" border="0">
 <tr>
 <td>Student name</td>
 <td><input type="text" name="fname" value="<?php echo $fname;?>" /></td>
 <td>Exam name</td>
<td><input type="text" name="exam_name" value="<?php echo $exam_name;?>" /></td>
<td>Class</td>
<td><input type="text" width="10" name="class" value="<?php echo $class;?>" /></td>

<td>Date</td>
<td><input type="date" name="date" value="<?php echo $date;?>" /></td>

<td><input type="submit" name="submit" value="Search" /></td>
 </tr>
</table>
 <br />
</form>
<table>
 <h4>
 <?php echo $exam_name;?>&nbsp;examinaton result of&nbsp;<?php echo $fname;?>  
    &nbsp;held    on&nbsp;<?php echo $date;?></h4><hr> 
 <table width="1110" border="0" cellpadding="2" cellspacing="1" >
<tr>
    <td bgcolor="#CCCCCC" width="2000">Student name</td>
    <td bgcolor="#CCCCCC" width="10">Mathematics</td>
      <td bgcolor="#CCCCCC" width="10">English</td>
       <td bgcolor="#CCCCCC" width="10">Kiswahili</td>
        <td bgcolor="#CCCCCC" width="10">Geograph</td>
        <td bgcolor="#CCCCCC" width="10">Civics</td>
          <td bgcolor="#CCCCCC" width="10">French</td>
          <td bgcolor="#CCCCCC" width="10">Religion</td>
            <td bgcolor="#CCCCCC" width="10">Pds</td>
             <td bgcolor="#CCCCCC" width="10">Vskill</td>
             <td bgcolor="#CCCCCC" width="10">Ict</td>
              <td bgcolor="#CCCCCC" width="10">Science</td>
                <td bgcolor="#CCCCCC" width="10">History</td>
                 <td bgcolor="#CCCCCC" width="10">Average</td>
                  <td bgcolor="#CCCCCC" width="10">Total</td>
    <td bgcolor="#CCCCCC" width="10">Exam_name</td>
    <td bgcolor="#CCCCCC" width="1000">Class</td>
    <td bgcolor="#CCCCCC" width="1000">Date</td>
</tr>
<?php
while($res=mysql_fetch_array($q)){
?>

<tr>
    <td width="100"><?php echo $res['fname'];?></td>
     <td width="10"><?php echo $res['mathematics'];?>%</td>
     <td width="10"><?php echo $res['english'];?>%</td>
     <td width="10"><?php echo $res['kiswahili'];?>%</td>
     <td width="10"><?php echo $res['geograph'];?>%</td>
     <td width="10"><?php echo $res['civics'];?>%</td>
    <td width="10"><?php echo $res['french'];?>%</td>
    <td width="10"><?php echo $res['religion'];?>%</td>
    <td width="10"><?php echo $res['pds'];?>%</td>
    <td width="10"><?php echo $res['vskill'];?>%</td>
    <td width="20"><?php echo $res['ict'];?>%</td>
    <td width="10"><?php echo $res['science'];?>%</td>
    <td width="10"><?php echo $res['history'];?>%</td>
    <td width="30"><?php echo $res['Average']; ?></td>
    <td width="30"><?php ?></td>
    <td width="10"><?php echo $res['exam_name'];?></td>
    <td width="30"><?php echo $res['class'];?></td>

    <td width="280" style="border-width:medium"><?php echo $res['date'];?></td>
</tr>
<?php }?>
  • 1
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Nov 16 '13 at 02:15

1 Answers1

0

At the top, do this:

$total_average = 0;
$total_num = 0;

Each iteration, do this:

$total_average += $res['Average'];
$total_num++;

At the bottom, do this:

echo $total_average; // or if you want to average the total average over total num:
echo $total_average / $total_num;

EDIT

To get the total for each subject, you need to add the value each time you loop to a variable. Here's an example, which you'll do for each of your subjects:

$total_math = 0;

while($res=mysql_fetch_array($q)){
    $total_math += $res['mathematics'];
    // the rest of your code goes here, along with the rest of your totals
}

echo 'The total for math is: ' . $total_math;
scrowler
  • 24,273
  • 9
  • 60
  • 92