...

View Full Version : Reversing the Order of CSV File in a Table



etocaj
02-18-2010, 03:25 PM
I use the following script to update a table from a csv file that is auto uploaded to my website every minute:



echo "<table align='left' width='685' bordercolor='ccc' border='1'>";
$file = "myfile.csv";
$content=file("$file");
foreach($content as $line){$row=explode(",",$line);

echo "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$row[7]}</td></td>";}

echo "</table>";?>


I need to modify this script so that the latest data is shown at the top. As it is now, you have to scroll to the bottom to see the latest data.

I have searched the forum for an answer to this but have had no luck.

Any help would be very much appreciated.

Thanks!

Fumigator
02-18-2010, 03:54 PM
You can use array_multisort() for this. Have a look:

http://us3.php.net/manual/en/function.array-multisort.php

Study especially Example #3 on that page, it's exactly what you need to do.

etocaj
02-18-2010, 06:04 PM
Thanks I reworked the code but it doesn't seem quite right and no longer displays.

I'm unsure what the problem is.

Here is the updated code:



echo "<table align='left' width='685' bordercolor='ccc' border='1'>";
$file = "myfile.csv";
$content=file("$file");
foreach($content as $line){$row=explode(",",$line);
array_multisort($row[0], SORT_ASC, $content);
echo "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$row[7]}</td></td>";}
echo "</table>";?>


Any ideas, I tried replacing this line with the format from the link but no luck, as the table wouldn't display:



foreach($content as $line){$row=explode(",",$line);


I just learning php so the curve for me is pretty steep right now.

Fumigator
02-18-2010, 06:13 PM
You need to follow the example closely:



<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>


Notice the first thing you need to do is create a new array based on the element in your array that you want to sort on. In the example this array is called $volume; you should name yours based on what you are sorting on (probably date).

Then, after you do that, call array_multisort(). THEN, loop through the original array, which is now sorted, to create your HTML output.

etocaj
02-18-2010, 07:08 PM
I have tried this two ways but neither works. The second way I tried to nest the array_multisort call. Also, I am unclear whether I should use content and data or just content for the foreach lines.


echo "<table align='left' width='685' bordercolor='ccc' border='1'>";
$file = "myfile.csv";
$content=file("$file");
foreach ($data as $key => $row) {
$date[$key] = $row['0'];
array_multisort($date, SORT_ASC, $data);}
foreach($content as $line) {
$row=explode(",",$line);
echo "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$row[7]}</td></td>";}echo "</table>";
?>



echo "<table align='left' width='685' bordercolor='ccc' border='1'>";
$file = "myfile.csv";
$content=file("$file");
foreach($content as $line) {
foreach ($data as $key => $row) {
$date[$key] = $row['0'];
array_multisort($date, SORT_ASC, $data);}
$row=explode(",",$line);
echo "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$row[7]}</td></td>";}echo "</table>";
?>

Fumigator
02-18-2010, 07:28 PM
I may have sent you down a path that is more complicated than it needs to be. If you only want to reverse the display of the data and don't really care about actually sorting the data, simply read your $content array from bottom to top using a "for" loop. Assign the iterator to the last element in your $content array and decrement the iterator as you go, like this:



for ($i = count($content) - 1; $i >= 0; $i--) {
$row = explode(",", $content[$i]);
//echo your html output here
}

etocaj
02-18-2010, 07:56 PM
Thanks that worked except the head row which was on top is now on bottom.

Always something!

I figured that would happen once I got it to sort in ascedning order by date.

If I change the 0 to a 1 in the above code I lose the header altogether.

I guess I could style antoher section of the table with manual headings unless there is an easy fix to keep the headings from being sorted too.

Edit: Here is the finalized code, I simply added the header manually.



<?php
echo "<table align='left' width='685' bordercolor='ccc' border='1'>";
echo "<tr><td><strong>Date</strong></td><td><strong>MPSAS</strong></td><td><strong>NELM</strong></td><td><strong>Temp (C)</strong></td></td>";

$file = "myfile.csv";
$content=file("$file");
for ($i = count($content) - 1; $i >=1 ; $i--) {
$row = explode(",", $content[$i]);

echo "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$row[7]}</td></td>";}
echo "</table>";
?>


Thanks a lot for your help and patience, I appreciate it!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum