Results 1 to 4 of 4

Thread: howto: sort a csv file in numeric order

  1. #1

    howto: sort a csv file in numeric order

    I want to sort the csv file in numeric order using the coloum id
    I tried sort_numeric and it would not work maybe I was not using it correctly.
    I want to sort this dropdown list, numerically by it value.
    PHP Code:
    <option value="0"> Select your State </option>
    <?PHP
    //open csv file then display the result in the dropdown list
    $file_handle fopen("file1.csv""r");
    while (!
    feof($file_handle) ) {
    $line_of_text fgetcsv($file_handle1024);
    //define coloum needed to be displayed from the csv file.
    $index $line_of_text[0];
    $value $line_of_text[1];
    ?>
    <option value="<?php print $index?>"> <?php print $value?> </option>
    <?php
    }
    ?></select><?php
    fclose
    ($file_handle);
    ?>

  2. #2
    id use this awesome class over at google code - it has this capability built in:

    http://code.google.com/p/parsecsv-for-php/

  3. #3
    Code:
    $sorted_arr = Array();
    $i = 0;
    while (!feof($file_handle) ) 
    {
        $line_of_text = fgetcsv($file_handle, 1024);
        //define coloum needed to be displayed from the csv file.
        $sorted_arr[$i][0] = $line_of_text[0];
        $sorted_arr[$i][1] = $line_of_text[1];
        $i++;
    }
    ksort( $sorted_arr );
    
    for( $i = 0; $i < count($sorted_arr); $i++ )
    {
        echo " <option value='" .  $sorted_arr[$i][0] . "'>" .  $sorted_arr[$i][1] . "</option>";  
    }
    the world is getting more complex because people need simple solutions

  4. #4
    It is not sorting

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012