PHP - How can I make calculations with numbers I get from a text file?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



PHP - How can I make calculations with numbers I get from a text file?



So I have a task where I should read a text file in php, containing students IDs and their grades, separated by vertical lines. It looks something like that:


12345|4|2|1|0|4
45678|1|2|3|4|4
78901|5|5|5|5|4



The program should print the amount of students there are on the file, which I managed to do by simply echoing the amount of lines. However, I should also print the sum of the grades for each student, and I simply can't find a way to do it. Do I get the characters by order on the line or something similar? I've been struggling with this for a day now and I only got as far as this:


<?php

$file="exam.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle))
$line = fgets($handle);
$linecount++;



echo "total_students was $linecount:";
$homepage = file_get_contents('exam.txt');
echo $homepage;


?>



This code only prints the page and the amount of lines, nothing more.





Side note: Is there a particular reason as to why you're not using a database for this? It would making counting/summing a lot easier, not to mention managing the data.
– Funk Forty Niner
Aug 12 at 13:45






Ah, I see (and understand). Here's a Q&A that I found which may help you if you want to take a look at it and try something from it stackoverflow.com/q/17548443/1415724 - Which I found in a Google searching using the following keyword phrase: "sum values from a text file php" and contains more hits.
– Funk Forty Niner
Aug 12 at 13:49





Well thank you for that, but I was definitely not asking you to write my homework. As you see, I was simply asking if there is an operator counting symbols on the file and stuff like that. If I wanted someone to "write my homework" I would simply copy paste the task the way it's written. If I wanted people to do my tasks I wouldn't even write here, I would just ask someone else doing the course to copy their answer.
– n_at
Aug 12 at 13:52





Well, in this case, you made a valid point, I removed my down vote,
– Ray A
Aug 12 at 13:53





hats off @FunkFortyNiner
– Ray A
Aug 12 at 13:56




1 Answer
1



So after I was guided in the right direction I managed to come up with a solution!


<?php

$file="exam.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle))
$line = fgets($handle);
$linecount++;



echo "total_students was $linecount:n";


$lines = file('exam.txt');
$sum = 0 ;

foreach ($lines as $line)
$var = explode("

$average = $all / $linecount;
echo "The average score was $average points."

?>



I exploded each line as an array divided by the "|" symbol, then I printed the student ID from the [0] of the array, and summed the rest. After that I summed the sums and thus found the average.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard