Reading flat file and display data in tabular form | PHP - IProgramX

Q. Write a program to read a flat file “student.dat”, calculate the percentage and display the data from file in tabular format.(Student.dat file contains rollno, name, Syspro, TCS, CN, PHP, JAVA, BA ) 


HTML File:

<html><head><title>Exercise 3 Set B 1</title></head>
<body bgcolor="green">
<form action="ass3b1.php" method="POST">
<h1>Reading flat file and display data in tabular form.</h1>
<br>
<br>
<hr>
<h3>Enter directory name(containing file student.dat):<input type="text" name='d'></h3>

<pre>

<input type="submit" name="submit" value="submit">    
<input type="reset" name="reset'" value="reset">

</pre>
</form>

</body></html>


PHP Function:
NOTE: PHP function is saved as "ass3b1.php"


<?php
{
if (file_exists('student.dat'))

$f=fopen('student.dat','r');

echo "<br/><br/><br/>";

echo "<table border type=3 align=center>";
echo "<tr><td colspan=7 align=center><h3>Student Records.</h3></td></tr>";
echo "<tr><td align=center>Roll no.</td>";
echo "<td align=center>Name.</td>";
echo "<td align=center>Electronics</td>";
echo "<td align=center>Computer.</td>";
echo "<td align=center>Maths</td>";
echo "<td align=center>Total.</td>";
echo "<td align=center>Percentage.</td></tr>";
while(!feof($f))
{

$d= fgets($f);
$s=explode(' ',$d);

if(!empty($s[0]) && !empty($s[1]) && !empty($s[2]) && !empty($s[3]) && !empty($s[4]))
{

$m1=$s[2];
$m2=$s[3];
$m3=$s[4];
$total=$m1+$m2+$m3;
$p=($total/300)*100;

echo "<tr><td align=center>$s[0]</td>";
echo "<td align=center>$s[1]</td>";
echo "<td align=center>$m1</td>";
echo "<td align=center>$m2</td>";
echo "<td align=center>$m3</td>";
echo "<td align=center>$total</td>";
echo "<td align=center>$p %</td></tr>";
}
}
echo "</table>";
}

?>


Output:



Post a Comment

0 Comments