Search This Blog

Sunday 23 January 2022

13. PHP Program to read data from MySQL table and Calculate attendance percentage.

<!-- 13. PHP Program to read data from MySQL table and calculate attendance percentage -->

<html>
<head>
<title>Read data and update mysql table using php </title>
</head>
<body bgcolor=gray text=yellow>
<?php

$connection = mysql_connect("localhost", "root", ""); // Establish Connection with Server
$db = mysql_select_db("test", $connection); // Selecting Database
//MySQL Query to read data
$query = mysql_query("select * from prog13", $connection);
echo "<h3>Student Attendance Details</h3>";
echo "<table border=3 cellspacing=5>";
echo"<tr><th>RegNo</th><th>StudentName</th><th>Subject</th><th>ClassesHeld</th><th>ClassesAttended</th><th>AttendancePercentage</th></tr>";
while ($row = mysql_fetch_array($query))
{
    echo "<tr><td>".$row['RegNo']."</td>";
    echo "<td>".$row['StudentName']."</td>";
    echo "<td>".$row['Subject']."</td>";
    echo "<td>".$row['ClassesHeld']."</td>";
    echo "<td>".$row['ClassesAttended']."</td>";
    echo "<td>".$row['AttendancePercentage']."</td></tr>";

    $reg=$row['RegNo'];
    $CH=$row['ClassesHeld'];
    $CA=$row['ClassesAttended'];
    $per=($CA/$CH)*100;
    
    //Query for updation with calculated percentage.
        $query1 = mysql_query("update prog13 set AttendancePercentage='".$per."'where RegNo='".$reg."';",$connection);
    //Query to reset AttendancePercentage to zero.
        //$query1 = mysql_query("update prog13 set AttendancePercentage=0 where RegNo='".$reg."';",$connection);
    
}
echo "<table border=3>";
echo "<br><b>MySQL table updated successfully.</b>";
//Query for inserting values in prog13 table.
        //$query1 = mysql_query("insert into prog13 values('123','abcd','sub1',20,16,0);",$connection);
mysql_close($connection); // Closing Connection with Server
?>
</body>
</html>

No comments :

Post a Comment

FREE Hit Counters