1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Formatting CSV for import.

Discussion in 'PHP' started by jfontestad, Jan 28, 2016.

  1. #1
    Hello all.

    Was looking for a little help.

    I have a CSV file that looks like this:

    ,,,,,,,,,
    ,,,,,,,,,
    "UNIT A ""1/1/2016"" to ""1/17/2016"", 0 (activity level)",,,,,,,,,
    Description,Quantity,Weight,Cost,Daily Qty,Daily Wgt,Daily Cst,C Qty,C Wgt,C Cst
    UNIT A,"6,193","5,268 Lb",$ 0.00,364.29,310 Lb,$ 0.00,N/A,N/A,N/A
    1010 ,"1,356","1,464 Lb",$ 0.00,79.76,86 Lb,$ 0.00,N/A,N/A,N/A
    1050 ,119,95 Lb,$ 0.00,7.00,6 Lb,$ 0.00,N/A,N/A,N/A
    1210 ,449,534 Lb,$ 0.00,26.41,31 Lb,$ 0.00,N/A,N/A,N/A
    1310 ,597,131 Lb,$ 0.00,35.12,8 Lb,$ 0.00,N/A,N/A,N/A
    1810 ,"1,499",125 Lb,$ 0.00,88.18,7 Lb,$ 0.00,N/A,N/A,N/A
    1710 ,"1,050",483 Lb,$ 0.00,61.76,28 Lb,$ 0.00,N/A,N/A,N/A
    1510 ,167,334 Lb,$ 0.00,9.82,20 Lb,$ 0.00,N/A,N/A,N/A
    1410 ,471,"1,696 Lb",$ 0.00,27.71,100 Lb,$ 0.00,N/A,N/A,N/A
    2310 ,369,258 Lb,$ 0.00,21.71,15 Lb,$ 0.00,N/A,N/A,N/A
    2320 ,116,147 Lb,$ 0.00,6.82,9 Lb,$ 0.00,N/A,N/A,N/A
    ,,,,,,,,,
    ,,,,,,,,,
    "UNIT B""1/1/2016"" to ""1/17/2016"", 0 (activity level)",,,,,,,,,
    Description,Quantity,Weight,Cost,Daily Qty,Daily Wgt,Daily Cst,C Qty,C Wgt,C Cst
    UNIT B,"6,193","5,268 Lb",$ 0.00,364.29,310 Lb,$ 0.00,N/A,N/A,N/A
    1010 ,"1,356","1,464 Lb",$ 0.00,79.76,86 Lb,$ 0.00,N/A,N/A,N/A
    1050 ,119,95 Lb,$ 0.00,7.00,6 Lb,$ 0.00,N/A,N/A,N/A
    1210 ,449,534 Lb,$ 0.00,26.41,31 Lb,$ 0.00,N/A,N/A,N/A
    1310 ,597,131 Lb,$ 0.00,35.12,8 Lb,$ 0.00,N/A,N/A,N/A
    1810 ,"1,499",125 Lb,$ 0.00,88.18,7 Lb,$ 0.00,N/A,N/A,N/A
    1710 ,"1,050",483 Lb,$ 0.00,61.76,28 Lb,$ 0.00,N/A,N/A,N/A
    1510 ,167,334 Lb,$ 0.00,9.82,20 Lb,$ 0.00,N/A,N/A,N/A
    1410 ,471,"1,696 Lb",$ 0.00,27.71,100 Lb,$ 0.00,N/A,N/A,N/A
    2310 ,369,258 Lb,$ 0.00,21.71,15 Lb,$ 0.00,N/A,N/A,N/A
    2320 ,116,147 Lb,$ 0.00,6.82,9 Lb,$ 0.00,N/A,N/A,N/A

    and so on....

    I am looking to format this data into something like this :
    "UNITID","PRODUCTID","QTY"

    For each item listed on under that unit block.

    So it would basically have to look for the :
    ,,,,,,,,,
    ,,,,,,,,,

    To know that it's a new unit.
    Then get the first column of the third row within a block, "Unit X", and use it as UNITID, that is to be used for each following rows for unit identification, and then just grab the first and second column of each row for the prodid and qty columns.

    Any help would be greatly appreciated.

    Thanks!
     
    jfontestad, Jan 28, 2016 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    sarahk, Jan 28, 2016 IP
    ThePHPMaster likes this.
  3. jfontestad

    jfontestad Well-Known Member

    Messages:
    1,236
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    148
    #3
    @sarahk I am looking to parse the data I am needing, as another array will be created to import into a DB.

    I was just thinking that a function that would loop through each block and then add the data I am looking for to an array, I thought something like that would be a lot easier. Unfortunately my programming skills are not up to par with me thinking skills.
     
    jfontestad, Jan 28, 2016 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    It's the double handling I'm tying to avoid

    I'd just import into Excel, get rid of the columns you don't want and save again as a .csv
     
    sarahk, Jan 28, 2016 IP
  5. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #5
    Note: in following colums are 0 based indexed, that is first column will be 0, second will be 1, 3rd will be 2 and so on.
    If you need selective columns, us this code:

    
    $c = fopen("myfile.csv", "rb");
    while (($row = fgetcsv($c, 2048) !== FALSE)
    {
            print $row[0] ."<br/>"; //0 = first column. print first column of each row
            //and so on check all columns suiting your needs and
            //insert to database
    }
    fclose($c);
    Code (markup):
    I hope it helps.

    Stay well.....
     
    Vooler, May 12, 2016 IP