SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: PHP and CSV

  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question PHP and CSV

    Ok I have a script that reads a csv file and through a while loop it checks each line and inserts the rows into a db table.

    Now here is the dilemma. There is a field called email address. There might be multiple rows with the same email address. I need to be able to choose only one and not upload the rest. How can I do that?

    So to reiterate I need to read through a csv file, if multiple email address are found (the same) then I need to choose the first one and skip the rest with the same email address.

    Thanks
    Silly

  2. #2
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is my code:
    PHP Code:
    [size=2][color=#000000]$row = 1;[/color][/size]
    [size=2][color=#000000]$handle = fopen ("csvfiles/".$_FILES['swcsv']['name'],"r");
    while ($data fgetcsv ($handle10000",")) {[/color][/size]
     


  3. #3
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $mails = array();
    while(.....................)
     {
     if(!
    in_array($data[4], $mails)
      {
       
    // compile db add string
      
    }
     
    $mails[] = $data[4];
     } 
    some approach like that should do - see if the email addr (referenced as $data[4] - not overly sure on fgetcsv output so amend if needed) is in the mails array, if it isn't already - do the db bit. end by adding it to the slowly building array
    teckis - that's news to me.

  4. #4
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pootergeist
    PHP Code:
    $mails = array();
    while(.....................)
    {
    if(!
    in_array($data[4], $mails)
    {
    // compile db add string
    }
    $mails[] = $data[4];

    some approach like that should do - see if the email addr (referenced as $data[4] - not overly sure on fgetcsv output so amend if needed) is in the mails array, if it isn't already - do the db bit. end by adding it to the slowly building array
    I see so before reading the csv file, create the array called mail. If you find the email address in the array then dont insert info into db, but if it doesnt find it, it inserts the info and adds the email address to the array. Correct?


    Silly

  5. #5
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup
    teckis - that's news to me.


Bookmarks

Posting Permissions

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