Jump to content

[SOLVED] Need help working with flat text file to display data


husslela03

Recommended Posts

I have this code, the only thing left that i need to do is add a column to the end of the data and display averages from each of the rows...

 

the flat text file is formatted as follows:

 

Email:LastName:FirstName:Assign1,100:Assign2:50:Assign3,100

[email protected]:Doe:John:92:47:87

 

The numbers in the first row are the total amount of points the assignment is out of.  In the second row which consists of many rows, are the actual numbers that the student earned..

so for example, John Doe received 92/100 for Assignment 1.

 

What I need to do in the last row, is display the students average score for all the assignments.

How would i do that?

 

Here is the code i have below:

 

<?php

session_start();

$course=$_GET['class'];

$username=$_GET['teacher'];

 

print $course;

 

$firstname=$_SESSION['firstname'];

$lastname=$_SESSION['lastname'];

$teacher=$_SESSION['username'];

 

$file_open=rtrim($course);

$file=".txt";

$full_file=$file_open.$file; //creates full file name to open

$fp=fopen($full_file,'r');  //opens the file

$course_contents=fread($fp,filesize($full_file));

 

 

$lines= explode("\n",$course_contents);

 

 

 

?>

<html>

<head>

<link rel="stylesheet" type="text/css" href="SASI_style.css" />

 

</head>

<body>

<?php?>

<table width=99% border=0>

<tr height=100 bgcolor="#8470ff">

    <td align="center" colspan=2> SASI Student Services </td>

    </tr>

    <tr>

      <td align="center" colspan=2>

      <?php echo 'Welcome ' .$firstname. ' ' .$lastname. '<br> Student Information Regarding: ' .$course;?> </td>

    </tr>

    </table>

      <center>

      <table style='font-family:times; text-align:left; font-size:12px;' width=100%>

      <?php

      $row=1;

      $handle= fopen($full_file,'r');

      $heading=True;

      $totals=array();

     

      while (($data=fgetcsv($handle,1024,":")) !==FALSE)

      {

        $colnum=0;

        print "<tr>\n";

        foreach($data as $col)

        {

          //header row

          if($row==1)

          {

            $total=explode(",",$col);

            $totals[]=$totals[1];

            $col=$total[0];

          }

          else

          {

            $percentage=is_numeric($totals[$colnum]) ? "(".(($col /$totals[$colnum])*100). "%)" : false;

          }

          echo "<t".(($row == 1) ? "h" : "d").">".$col.((is_numeric($totals[$colnum]) && $row > 1) ? " / ".$totals[$colnum] : false).$percentage."</t".(($row == 1) ? "h" : "d").">\n";

          $colnum++;

        }

        print "</tr>\n";

        $row++;

      }

      fclose($handle);

      echo '</table>';

      ?>

      <table>

      <tr>

      <td align="center">

      <?php

      echo "<a href = \"SASSI_main.php\">Logout</a>";

      ?>

      </td>

      </tr>

      </table>

      </center>

      </html>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.