www.webdeveloper.com
Results 1 to 5 of 5

Thread: read csv file

  1. #1
    Join Date
    Nov 2008
    Posts
    61

    read csv file

    Hi!
    I create a program that read each line of csv file and insert data into database.
    My proble is to intercept first line and if the cell contents match certain values can then start the database on insert

    for example
    colum a,1 value NAME
    colum b,1 value CITY
    colum c,1 value PHONE

    I need to test this:

    if ((colum a,1=='NAME') && (colum b,1=='CITY') && (colum c,1=='PHONE')){
    ..query for insert
    }

    My code is something like this:


    if (($fp = fopen($new_file, "r")) !== false)
    {

    while (($data = fgetcsv($fp, 1000, $delimiter)) !== false) {

    $name=trim($data[1]);
    $city=trim($data[2]);
    phone=trim($data[3]);

    $insert=" ... ... ";
    }
    }

    but I can't able to add test for first line
    I hope it's clear..
    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,359
    You can make your first call to fgetcsv() before the while() loop, process that line as desired, and then continue the rest of the file within that while() loop.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Nov 2008
    Posts
    61
    sorry..but it's not clear..can you make an example please..

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,359
    PHP Code:
    $data fgetcsv($fp1000$delimiter);

    // do special handling of 1st line here

    while (($data fgetcsv($fp1000$delimiter)) !== false) {

       
    // do "normal" handling of all subsequent lines here


    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2013
    Location
    Surendranagar Gujarat India
    Posts
    55
    The above CSV file is the one we want to work with. It shows each line separated by commas.

    PHP has a function that allows you to work with CSV file. It's called fgetcsv( ). It's just like the fgets function you used earlier. The difference is that fgetcsv( ) separates each line on the commas, and puts each part into an array. Before trying the next code, make sure you copy the widgets.csv file, from the ones you downloaded, over to the same folder where your script is. The widgets.csv file is in the scripts folder. Here's the code:

    <?PHP

    $file_handle = fopen("widgets.csv", "r");

    while (!feof($file_handle) ) {

    $line_of_text = fgetcsv($file_handle, 1024);

    print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";

    }

    fclose($file_handle);

    ?>

    When you run the script, you should see all the lines of text printed out. Here's how it works.

    The first two lines you've already met: get a handle to the file you want to open, then create a while loop to loop round it.

    $file_handle = fopen("widgets.csv", "r");

    while ( !feof($file_handle) ) {

    }

    Inside the while loop, though, we have our fgetcsv line:

    $line_of_text = fgetcsv( $file_handle, 1024 );

    In between the round brackets of fgetcsv, we've typed to things: our file handle, and the size of the line to read. The size, in this case, is 1024 - 1 kilobyte. You can change this value. When the function gets the line of text, it puts it into the variable we've called $line_of_text.

    But $line_of_text will now be an array. Each line of our text file looks like this:

    Widget1, blue, £10

    The fgetcsv function will split this line when it sees the comma. It then creates a position in the array to hold each part of the line. So it will set up an array with three positions, in our case. Those positions will be as follows:

    $line_of_text[0]
    $line_of_text[1]
    $line_of_text[2]

    To print it all out, we had a this line:

    print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";

    All we're doing here is to print out what is in each position of the array. We've then added a HTML line break, just for display purposes.

    If you have CSV files from a spreadsheet that you need to display on a web page, the fgetcsv function can come in quite handy!



    In the next part, you'll see other options for reading a text file line by line


    Kishor Makwana
    Software Engineer
    Insight Softech
    www.insightsoftech.com

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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



Recent Articles