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 6 of 6
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parsing CSV files using foreach / arrays / fgetscsv

    I'm trying to avoid using MySQL by using my CSV file on the server. I need to pull out my students information from each class based on their age. I've been reading about the use of 'fgetscsv' but would not know how to apply it.

    Here's how the csv file looks like
    (TEACHER,AGE,SUBJECT,NAME)

    MS James,10,math,john
    MS James,10,math,jamie
    MS James,9,english,judy
    MS James,10,english,jordan
    MS James,9,english,david
    MR Henington,10,english,tina
    MR Henington,9,math,mike
    MR Henington,9,math,mathew

    Example:
    I'm trying to echo all the data that has students that are 10 years old to come out like this:

    "
    Hi I'm John and I'm 10 years old. My favorite subject is math
    Hi I'm Tina and I'm 10 years old. My favorite subject is english
    etc.
    "


    Any help would be appreciated.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,077
    Thanks
    2
    Thanked 320 Times in 312 Posts
    The following generic code will read your file into an array of arrays -
    PHP Code:
    <?php
    $handle 
    fopen("curb.csv""r");
    $data = array();
    while ((
    $line fgetcsv($handle)) !== FALSE) {
    $data[] = $line;
    }
    ?>
    The $data array will look like this -
    Code:
    Array
    (
        [0] => Array
            (
                [0] => MS James
                [1] => 10
                [2] => math
                [3] => john
            )
    
        [1] => Array
            (
                [0] => MS James
                [1] => 10
                [2] => math
                [3] => jamie
            )
    
        [2] => Array
            (
                [0] => MS James
                [1] => 9
                [2] => english
                [3] => judy
            )
    
        [3] => Array
            (
                [0] => MS James
                [1] => 10
                [2] => english
                [3] => jordan
            )
    
        [4] => Array
            (
                [0] => MS James
                [1] => 9
                [2] => english
                [3] => david
            )
    
        [5] => Array
            (
                [0] => MR Henington
                [1] => 10
                [2] => english
                [3] => tina
            )
    
        [6] => Array
            (
                [0] => MR Henington
                [1] => 9
                [2] => math
                [3] => mike
            )
    
        [7] => Array
            (
                [0] => MR Henington
                [1] => 9
                [2] => math
                [3] => mathew
            )
    
    )
    You would need to loop through each element of the $data array, testing the age element as you go, and produce the output that you want.

    Edit: You could also put a test inside of the while loop that only returns the lines you want, then loop through the resultant lines and produce the output that you want.
    Last edited by CFMaBiSmAd; 02-23-2007 at 09:02 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thankyou for the fast response. I tried out the code but it's giving me this error:

    Warning: Wrong parameter count for fgetcsv()

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,077
    Thanks
    2
    Thanked 320 Times in 312 Posts
    I'm guessing you are using PHP4. The length parameter is optional in PHP5.

    Change the line of code as follows (the length must be longer than your longest single line in the file) -
    PHP Code:
    while (($line fgetcsv($handle1000)) !== FALSE) { 
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How do I pull and sort out only rows that contains a specific age from "$data[1]"?



    echo "<table>";

    $handle = fopen("students.csv", "r");
    $data = fgetcsv($handle, 1000, ",");
    # data rows:
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {

    $teacher=$data[0];
    $age=$data[1];
    $subject=$data[2];
    $name=$data[3];



    echo "<tr>";

    echo "<td>Hi I\m $name and I\'m $age years old. My favorite subject is $subject.</td>";

    echo "</tr>\n";
    }
    echo "</table>\n";

  • #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 think an "if" statement will do the trick.


  •  

    Posting Permissions

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