husslela03 Posted November 17, 2009 Share Posted November 17, 2009 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 doe@email.com: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> Quote Link to comment https://forums.phpfreaks.com/topic/181889-solved-need-help-working-with-flat-text-file-to-display-data/ Share on other sites More sharing options...
taquitosensei Posted November 17, 2009 Share Posted November 17, 2009 this should do it <td><?php echo (is_numeric($data[3]) && is_numeric($data[4]) && is_numeric($data[5]))?sprintf("%1\$.2f", ((($data[3]+$data[4]+$data[5])/3)/1)).'%':""; ?></td> Quote Link to comment https://forums.phpfreaks.com/topic/181889-solved-need-help-working-with-flat-text-file-to-display-data/#findComment-959341 Share on other sites More sharing options...
husslela03 Posted November 17, 2009 Author Share Posted November 17, 2009 it does work! where should i place it in the code? Quote Link to comment https://forums.phpfreaks.com/topic/181889-solved-need-help-working-with-flat-text-file-to-display-data/#findComment-959384 Share on other sites More sharing options...
husslela03 Posted November 17, 2009 Author Share Posted November 17, 2009 nevermind! I got it! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/181889-solved-need-help-working-with-flat-text-file-to-display-data/#findComment-959388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.