0

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.

Jordan
  • 97
  • 3
  • 12

1 Answers1

1

From what i understand, you want to order the data by the first column before push the in the menu.

You can try to order with the function 'usort', with a callback function. You can try this code:

//This function compare the array rows
function cmp_rows($a, $b){
    if ($a[0] == $b[0]) {
        return 0;
    }
    return ($a[0] < $b[0]) ? -1 : 1;
}

$htmlMenu .= "<tr> <th> Name/ID </th> <th> URL </th> <th> Text </th> </tr>";

//get all the .csv files in paht $filePath
$filePath = "/path/to/the/files";
$files = glob($filePath."/*.csv");

$date = array();
// Iterate each file
if(count($files)) foreach($files as $file){
    if(($file_handle = fopen($fullCSVUrl, "r")!==FALSE){
        while (!feof($file_handle) ) {
            $data[] = fgetcsv($file_handle, 1024);
        }
        fclose($file_handle);
    }else{
        echo "<li> Could not open file ".$file;
    }
}

//Order the array using the compare function 'cmp_rows', previously defined
usort($data,"cmp_rows");

if(count($data)) foreach($data as $line_of_text){
    $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>";
megavexus
  • 131
  • 4
  • Thanks for the reply, this code is within a loop as there are multiple files. Your code does sort the database but now all the printed out databases are just 1.csv printed over and over again - albeit sorted. How can this be modified to support multiple uses within my loop (the one all of my code is contained in) – Jordan Dec 14 '16 at 13:46
  • First you have to put all the files in an array, and then you have only to iterate between them. You can try something like this: http://stackoverflow.com/questions/29608709/reading-csv-files-from-directory-and-showing-the-content-of-each-file-fails I will edit the answer to makea a better approach to your problem... – megavexus Dec 14 '16 at 19:03