husslela03 Posted October 29, 2009 Share Posted October 29, 2009 Hello, I have a flat text file in this format: Email:LastName:FirstName:Assignment1,100:Assignment2,100;Quiz1,50 someone@email.com:Doe:John:95:86:40 someone_else@email.com: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. Quote Link to comment https://forums.phpfreaks.com/topic/179493-need-help-working-with-flat-text-file/ Share on other sites More sharing options...
JonnoTheDev Posted October 29, 2009 Share Posted October 29, 2009 So for John Doe you would divide 100 by 95 to give you 1.05263 for assignment 1 and display it how? Quote Link to comment https://forums.phpfreaks.com/topic/179493-need-help-working-with-flat-text-file/#findComment-947063 Share on other sites More sharing options...
husslela03 Posted October 29, 2009 Author Share Posted October 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179493-need-help-working-with-flat-text-file/#findComment-947076 Share on other sites More sharing options...
JonnoTheDev Posted October 29, 2009 Share Posted October 29, 2009 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 someone@email.com:Doe:John:95:86:40 someone_else@email.com: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> Quote Link to comment https://forums.phpfreaks.com/topic/179493-need-help-working-with-flat-text-file/#findComment-947087 Share on other sites More sharing options...
husslela03 Posted October 29, 2009 Author Share Posted October 29, 2009 hi, This worked really great. The only thing is, is it is not displaying the overall average column on the very right side... I need to add up the values in each row and display the overall average out of the points possible. Quote Link to comment https://forums.phpfreaks.com/topic/179493-need-help-working-with-flat-text-file/#findComment-947312 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.