Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Reversing the Order of CSV File in a Table

    I use the following script to update a table from a csv file that is auto uploaded to my website every minute:

    PHP Code:
    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!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can use array_multisort() for this. Have a look:

    http://us3.php.net/manual/en/functio...-multisort.php

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

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:

    PHP 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:

    PHP Code:
    foreach($content as $line){$row=explode(",",$line); 
    I just learning php so the curve for me is pretty steep right now.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You need to follow the example closely:

    PHP Code:
    <?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($volumeSORT_DESC$editionSORT_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.

  • #5
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

    PHP Code:
    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($dateSORT_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>";
    ?> 
    PHP Code:
    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($dateSORT_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>";
    ?> 

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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:

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


  • Users who have thanked Fumigator for this post:

    etocaj (02-18-2010)

  • #7
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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 >=$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!
    Last edited by etocaj; 02-18-2010 at 09:12 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •