so I have to make a PHP program for an assignment. The task is to take some CSV data from a .txt file (Student ID, Name, Surname). Sort the data alphabetically, divide it in 2 and put the data into 2 files which represent 2 workshop classes. We are not allowed to edit the original data.
My code works but has a few bugs which I am struggling to fix. Here is the original data form the .txt file:
123, bill, bobs
124, joe, public
125, amy, student
126, jane, doe
127, Ian, Dear
129, john,stonham
132, ali, mousavi
128, John, doe
130, bob, vinder
133, john, smith
134, joe, brown
138, jock, mcphelan
140, taffy, willians
145, harry, agius
146, rafiq,swash
157, angelina, karpovich
160, erica, elliott
162, steve, cockett
163, fred, weimer
165, jack, hass
167, paddy, o'reilly
Here is my code for the solution:
$students = array();
$groupNames1 = array();
$groupNames2 = array();
//Import and setup files for import and export
$file_handle = fopen("students.txt", "r") or die("unable to open file!");
$group1 = fopen("group_1.txt", "w") or die("Unable to create file!");
$group2 = fopen("group_2.txt", "w") or die("Unable to create file!");
//Exploding csv format to extract contents into array
while (!feof($file_handle)) {
$row = fgets($file_handle);
$explode = explode(",", $row);
$groupCountmportArray = array ('lName' => capitalise($explode[2]) , 'fName' => capitalise($explode[1]), 'id' => $explode[0]);
$students[] = $groupCountmportArray;
}
fclose($file_handle);
//Arrange in alphabetical order
array_multisort($students);
//Divide array into 2 groups, sorting the remainder
$arrayDivide = round(sizeof($students)/2)-1;
//Checks to see if Group1 is the right size, if not adds another student until condition is met
$groupCount = 0;
while($groupCount < $arrayDivide){
$groupNames1[] = $students[$groupCount];
$groupCount++;
}
//Checks to see if Group2 is the right size, if not adds another student until condition is met
while($groupCount < sizeof($students)){
$groupNames2[] = $students[$groupCount];
$groupCount++;
}
//Adds Group1 students to the Group 1 file
$group1Write = 0;
while($group1Write < sizeof($groupNames1)){
$txt = $groupNames1[$group1Write]['id'] . "," . $groupNames1[$group1Write]['fName'] . "," . $groupNames1[$group1Write]['lName'] ;
fwrite($group1, $txt);
$group1Write++;
}
fclose($group1);
//Adds Group2 students to the Group 2 file
$group2Write = 0;
while($group2Write < sizeof($groupNames2)){
$txt = $groupNames2[$group2Write]['id'] . "," . $groupNames2[$group2Write]['fName'] . "," . $groupNames2[$group2Write]['lName'] ;
fwrite($group2, $txt);
$group2Write++;
}
fclose($group2);
//Capitalise the names in the file
function capitalise ($name){
return ucwords($name);
}
//Displays message to let the user know that the code has run and the files have been created
echo "<h2>File Made. Please check your 'www' Folder on the C: Drive.</h2>";
Here is the output:
File 1 (Works fine):
145, Harry, Agius
123, Bill, Bobs
134, Joe, Brown
162, Steve, Cockett
127, Ian, Dear
126, Jane, Doe
128, John, Doe
160, Erica, Elliott
165, Jack, Hass
157, Angelina, Karpovich
File 2 (Has the Problem):
138, Jock, Mcphelan
132, Ali, Mousavi
167, Paddy, O'reilly124, Joe, Public
133, John, Smith
125, Amy, Student
130, Bob, Vinder
163, Fred, Weimer
140, Taffy, Willians
129, John,Stonham
146, Rafiq,Swash
So the problem with the second file is that Paddy O'Reilly and Joe Public are on the same line. But also, because John Stonham and Rafiq Swash are positioned next to the comma in the original, it is affecting the sort function for the array, therefore making them output incorrectly.
Any help on this would be greatly appreciated, I have been slaving over this for hours and do not know which other way to turn.
Thanks
The input file apparently doesn't end with a newline. fgets()
will return a string ending with newline if the line has a newline at the end, but it won't add a newline if there isn't one. So you need to add it yourself.
To fix the problem with the sorting of Stonham and Swash, you need to sort without including the spaces. The simplest way to achieve this is to remove the spaces at the beginning of each field.
while ($row = fgets($file_handle)) {
if ($row[strlen($row)-1] != "
") {
$row .= "
";
}
$groupCountmportArray = array (
'lName' => capitalise(ltrim($explode[2])) ,
'fName' => capitalise(ltrim($explode[1])),
'id' => $explode[0]
);
$students[] = $groupCountmportArray;
}