Alina Khachatrian Alina Khachatrian - 26 days ago 10
PHP Question

PHP script that reads txt file line by line, sorting it in agreed format and calculates statistic

A bbm.txt file contain:

BBM, PHP, John Smith, 16/11/2016

12345678 56

34567822 67

12324654 98

You should get

File name: bbm.txt

Module code: BBM

Module Name: PHP

Tutor: John Smith

The award date: 16/11/2016

12345678: 56

34567822: 67

12324654: 98

And I get

BBM PHP John Smith 16/11/2016

12345678 56

34567822 67

12324654 98

Please, need help.

function readLineByLine () {
if (file_exists('bbm.txt')){
$tmaFileToOpen = fopen("bbm.txt", "r"); // open our bbm.txt file
if ($tmaFileToOpen) { // true if no errors occured
while(!feof($tmaFileToOpen)) {
// variable $result will display data in line by line
$result = fgets($tmaFileToOpen, 1024);
$split = explode(',', $result);
echo '<p>' . $split[0] . $split[1] . $split[2] . $split[3] . '</p>';
}
} else { // if an error occured message will be displayed
echo '<p>There is an error openning file!</p>';
}
fclose($tmaFileToOpen); // Close our bbm.txt file
} else { // if file doesn't exist message will be displayed
echo '<p>File requested not found!</p>';
}
}
echo readLineByLine();

Answer

The comment above is correct:

'<p>Module Code:' . $split[0] .'<br>Module Name: '. $split[1] .'<br>Tutor'. $split[2] .'<br>The award date: '. $split[3] . '</p>';