Jump to content

Need help working with flat text file


husslela03

Recommended Posts

Hello,

 

I have a flat text file in this format:

 

Email:LastName:FirstName:Assignment1,100:Assignment2,100;Quiz1,50

[email protected]:Doe:John:95:86:40

[email protected]:Doe:Jane:80:96:45

 

I already used the fgetcsv() function to set it up as tables where the first row is the header of each column...

what i need to do next, is some way, take the weight of the assignment as you can see up top for Assignment 1 for example is 100 delimited with a "," instead of a ":" and then divide it by the person's actual score and display it

next to their score for the assignment. 

 

At the end of each row, for each person, I would like to add up the total points possible from the first row, and then divide it by the points actually earned by each student.

 

Can someone help me?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/179493-need-help-working-with-flat-text-file/
Share on other sites

For John doe I would divide 95 by the 100 in the first row for Assignment 1 and display it as a percentage next to his score ...then at the end of John Doe's row add up all his scores, and divide it by the total points possible from Row 1 for each assignment.

 

But also see, each text file is set up the same and I have multiples of these text files.  So I have to be able to figure out a way to do a loop that goes through the row and looks for something to the right of a delimiter of "," and use that to divide each score by and then add the array at the end.

 

Try this out. Be careful. Your csv data has an error. The last delimiter should be : not ;

Email:LastName:FirstName:Assignment1,100:Assignment2,100:Quiz1,50
[email protected]:Doe:John:95:86:40
[email protected]:Doe:Jane:80:96:45

<table style='font-family:arial; text-align:left; font-size:12px;' width="100%">
<?php
$handle = fopen("test.csv", "r");
$row = 1;
$totals = array();
while(($data = fgetcsv($handle, 1000, ":")) !== FALSE) {
$colnum = 0;
print "<tr>\n";
foreach($data as $col) {
	// header row
	if($row == 1) {
		$total = explode(",",$col);
		$totals[] = $total[1];
		$col = $total[0];
	}
	else {
		$percentage = is_numeric($totals[$colnum]) ? " (".(($col / $totals[$colnum]) * 100)."%)" : false;
	}
	print "<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);
?>
</table>

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.