Thread: Fgetcsv help!

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Fgetcsv help!


    im stuck in a bubble, im trying to learn php and here is where im stuck.

    im learning to read a .csv file. here is my code

    PHP Code:
    <?php

    $fh 
    fopen("courses.csv"'r') or die("File does not exist or you lack permission to open it");

    $heading fgetcsv($fh1000",");

        echo 
    $heading;

    if ((
    $handle fopen("courses.csv""r")) !== FALSE) {
        while ((
    $data fgetcsv($handle1000",")) !== FALSE) {
            
    $num count($data);

            for (
    $c=0$c $num$c++) {
                echo 
    $data[$c] . "<br />\n";
            }
        }
        
    fclose($handle);
    }


    ?>
    and i get this:

    http : / / s7.postimage.org/4eevdxil7/Screen_shot_2013_02_19_at_7_19_57_PM.png

    and what im trying to accomplish is in this picture

    http: / / s12.postimage.org/oqwiz6h6l/image.jpg

    i separated the forward slashes
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,905
    Rep Power
    3969
    The first line is going to be your field names, so you need to read that and save the names for use later. It appears you have done this with your $heading variable.

    After you've got the headers, you just keep reading the file line by line by calling fgetcsv in your loop. You should not be re-opening before the loop, as that will cause you to re-read the header row.

    For each line you read with fgetcsv, you need to loop over the field names you got previously and echo them out + the appropriate value from that row. The value for a given field will be at the same index in the row's array as the header is in the heading array.

    Finally after each row, echo a <hr> to create the separator line.

    For example:

    Code:
    $fh = fopen("courses.csv", 'r') or die("File does not exist or you lack permission to open it"); 
    
    $heading = fgetcsv($fh, 1000, ","); 
    
    while (($data = fgetcsv($fh, 1000, ",")) !== FALSE) { 
       foreach ($heading as $idx=>$title){
          echo "<strong>{$title}</strong>: {$data[$idx]}<br>\n";
       } 
    
       echo "<hr>";
    } 
    fclose($fh);
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by kicken
    The first line is going to be your field names, so you need to read that and save the names for use later. It appears you have done this with your $heading variable.

    After you've got the headers, you just keep reading the file line by line by calling fgetcsv in your loop. You should not be re-opening before the loop, as that will cause you to re-read the header row.

    For each line you read with fgetcsv, you need to loop over the field names you got previously and echo them out + the appropriate value from that row. The value for a given field will be at the same index in the row's array as the header is in the heading array.

    Finally after each row, echo a <hr> to create the separator line.

    For example:

    Code:
    $fh = fopen("courses.csv", 'r') or die("File does not exist or you lack permission to open it"); 
    
    $heading = fgetcsv($fh, 1000, ","); 
    
    while (($data = fgetcsv($fh, 1000, ",")) !== FALSE) { 
       foreach ($heading as $idx=>$title){
          echo "<strong>{$title}</strong>: {$data[$idx]}<br>\n";
       } 
    
       echo "<hr>";
    } 
    fclose($fh);
    thanks for the help! and the explanation!

IMN logo majestic logo threadwatch logo seochat tools logo