I have a CSV file that stores the ID of an image in the first column, along with the url of said image and its label text (the spreadsheet is converted to an image gallery elsewhere). There are multiple CSV's named 1.csv, 2.csv, 3.csv and so on - for looping purposes I can then simply grab my loop counter concatenated to ".csv" to grab the file - as there will always be a set order of CSV's but not always a set amount.
When the user uses my control panel to edit the CSV, they can change the ID of an image, and when they add a new one they can specify its ID too.
This is an example of the CSV
1,images/url/photograph.jpg,label text
2,images/url/image2.jpg,label text
4,images/url/apple.jpg,label text
3,images/url/foo.jpg,label text
5,images/url/5.jpg,label text
Each CSV is converted in the following while loop, which itself exists within a loop that goes through each file one by one
$htmlMenu .= "<tr> <th> Name/ID </th> <th> URL </th> <th> Text </th> </tr>";
$file_handle = fopen($fullCSVUrl, "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
$htmlMenu .= "<tr> <td> $line_of_text[0] </td> <td> <a href=\"$line_of_text[1]\"> $line_of_text[1] </a> </td> <td> $line_of_text[2] </td>";
}
$htmlMenu .= "</table>";
Finally, $htmlMenu is echoed and it produces a page with each CSV as individual HTML tables one by one. I believe that this means $line_of_text[0] is the ID grabbed from the file.
I would like, within this loop if possible, to sort the data by the first column and then send it back to the CSV, effectively sorting the files by ID every time this page is visited. I don't mind either sorting the CSV itself or sorting the array and pushing it back into the file, replacing the old data.
I hope I've explained effectively, I know what I want to do and where, I just can't quite figure it out. Thank you for your help.
PS: I understand that the sorted table will need a refresh to display, this is fine as it is for my personal use.