0

I'm creating a table using PHP from a CSV file.

I am trying to sort the output (DESCENDING) by one column from the CSV file which is: $line_of_text[13]

But I have no idea how to this as it is not as straight forward as sorting the output if I was using mysql.

This is my entire code:

<?php

$file_handle = fopen("MY-CSV-FILE.csv", "r");

print "<table style='width:100%; float:left;'>\n";
while (!feof($file_handle) ) {




    //print '<tr>';
    $line_of_text = fgetcsv($file_handle, 1024);

$line_of_text = str_replace('http', '<a  href=""><img title="Click To Enlarge" class="fancybox" style="width:210px; height=210px; float:left;" src ="http', $line_of_text);
$line_of_text = str_replace('jpg', 'jpg"/></a>', $line_of_text);

$line_of_text = str_replace(',', '', $line_of_text);
$line_of_text = str_replace('PictureRefs', '', $line_of_text);

asort($line_of_text);

foreach($line_of_text as $key => $value)

    print "<tr><td>".$line_of_text[9].'</td><td>'.$line_of_text[10].'</td><td>'.$line_of_text[2].'</td><td>'.$line_of_text[5].'</td><td>'.$line_of_text[6].'</td><td>'.$line_of_text[7].'</td><td>'.$line_of_text[8].'</td><td>'.$line_of_text[3].'</td><td>'.$line_of_text[4].'</td><td>'.$line_of_text[11].'</td><td>'.$line_of_text[12].'</td><td>'.$line_of_text[13].'</td><td>'.$line_of_text[14].'</td><td>'.$line_of_text[15]."</td></tr>\n<tr><td width='100%' colspan='100'><div style=' width:100%;'>".$line_of_text[16]."</div></td></tr>";


}
print '</table>';
fclose($file_handle);
?>

As you can see I have started doing this:

asort($line_of_text);

foreach($line_of_text as $key => $value)

but I don't think this is correct!

could someone please advise on this ?

any help would be appreciated.

H.HISTORY
  • 490
  • 7
  • 27
  • possible duplicate of [Reference: all basic ways to sort arrays and data in PHP](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php) // Of course you need to put all lines into an array first, so not two loops nested into each other, but two separate ones after one another. – CBroe Aug 14 '15 at 18:37

1 Answers1

0

You need to load all of your rows into an array, sort, and then echo the table. You need to split this into two loops.

Blake A. Nichols
  • 832
  • 1
  • 6
  • 10