Link to home
Start Free TrialLog in
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

Need To Parse CSV file in PHP

I have a csv file I am trying to parse two values out of it.  The first value is array element 1 productcode and the second value is element 4 weight.  After I get these two values for each product I need to create a csv file of the results using the implode function.

Here is the file
[PRODUCTS]
!PRODUCTID;!PRODUCTCODE;!PRODUCT;!CLEAN_URL;!WEIGHT;!LIST_PRICE;!DESCR;!FULLDESCR;!KEYWORDS;!TITLE_TAG;!META_KEYWORDS;!META_DESCRIPTION;!AVAIL;!RATING;!FORSALE;!SHIPPING_FREIGHT;!FREE_SHIPPING;!DISCOUNT_AVAIL;!MIN_AMOUNT;!LENGTH;!WIDTH;!HEIGHT;!LOW_AVAIL_LIMIT;!FREE_TAX;!CATEGORYID;!CATEGORY;!POS;!MEMBERSHIP;!PRICE;!TAXES;!ADD_DATE;!VIEWS_STATS;!SALES_STATS;!DEL_STATS;!SMALL_ITEM;!SEPARATE_BOX;!ITEMS_PER_BOX;!MANUFACTURERID;!MANUFACTURER
889;"10810-7";"";10810-7;0;75;"";"";"";"";"";"";0;0;N;0;N;Y;1;0;0;0;10;N;75;"ADD & ADHD";0;;50;;Wednesday 01 June 2011 02:03:18 AM;0;0;0;Y;N;1;"0";
888;"14553-4";"";14553-4;0;15;"";"";"";"";"";"";0;0;N;0;N;Y;1;0;0;0;10;N;75;"ADD & ADHD";0;;10;;Wednesday 01 June 2011 02:03:18 AM;0;0;0;Y;N;1;"0";
903;"150-6";"";150-6;0;14.050;"";"";"";"";"";"";0;0;N;0;N;Y;1;0;0;0;10;N;75;"ADD & ADHD";0;;9.350;;Wednesday 01 June 2011 02:03:35 AM;0;0;0;Y;N;1;"0";
902;"16343-9";"";16343-9;0;0;"";"";"";"";"";"";0;0;N;0;N;Y;1;0;0;0;10;N;75;"ADD & ADHD";0;;174.950;;Wednesday 01 June 2011 02:03:34 AM;0;0;0;Y;N;1;"0";
890;"166-7";"";166-7;0;14.950;"";"";"";"";"";"";0;0;N;0;N;Y;1;0;0;0;10;N;75;"ADD & ADHD";0;;9.950;;Wednesday 01 June 2011 02:03:20 AM;0;0;0;Y;N;1;"0";
906;"1672-1";"";1672-1;0;23.650;"";"";"";"";"";"";0;0;N;0;N;Y;1;0;0;0;10;N;75;"ADD & ADHD";0;;15.750;;Wednesday 01 June 2011 02:03:38 AM;0;0;0;Y;N;1;"0";
896;"1958-8";"";1958-8;0;168.750;"";"";"";"";"";"";0;0;N;0;N;Y;1;0;0;0;10;N;75;"ADD & ADHD";0;;115;;Wednesday 01 June 2011 02:03:31 AM;0;0;0;Y;N;1;"0";

Open in new window


Here is the code I am using to display the file and take it out of the csv format
<?php
$file = fopen("weight_nsp_ben.csv","r");
while(! feof($file))
  {
  print_r(fgetcsv($file));
  }
fclose($file);
?>

Open in new window


My problem is I am not sure how to extract productcode and weight for each product. As and indexed array it is index 1 and 4.  Thanks for the help.

Randal
Avatar of hielo
hielo
Flag of Wallis and Futuna image

<?php
$data=array();
if (($handle = fopen("weight_nsp_ben.csv", "r")) !== FALSE)
{
    //fetch the "[products]" row, but do nothing with it
    fgetcsv($handle, 1000, ';');

    //fetch the headers row
    $headers = fgetcsv($handle, 1000, ';');

    //locate the index of product_code and weight
    $product_code=array_search('!PRODUCTCODE',$headers);
    $weight=array_search('!WEIGHT',$headers);

    while (($row = fgetcsv($handle, 1000, ";")) !== FALSE) 
    {
        $data[]=sprintf('"%s","%s"',$row[$product_code], $row[$weight]);
    }
    fclose($handle);

    file_put_contents(getcwd().'/result.csv', implode(PHP_EOL,$data) );
}
?>

Open in new window

Avatar of sharingsunshine

ASKER

I am getting this error on line 38 which is
file_put_contents(getcwd().'/result.csv', implode(PHP_EOL,$data) );

Warning: file_put_contents(/Users/abx/Sites/sandbox/result.csv): failed to open stream: Permission denied in /Users/abx/Sites/sandbox/test_weight.php on line 38

I created result.csv when the error first occurred but that didn't change anything.  I even chmodded it to 755.
Avatar of duncanb7
duncanb7

Dear sharingsunshine,

you can use explode() , file(),file_put_contents function in php to finish your work , and those
functions are  easily to use and mentioned in http://hk2.php.net/file_put_contents

the result can be saved into output.csv file by file_put_contents,

I just make simple code to finish your work, and it works on my server but not fully tested, you need to do the rest

Hope understand your question completely, if not please pt it out.

Duncan

<?php
$proverbs=file("weight_nsp_ben.csv");$cnt=count($proverbs);$str="";

//-Finding product tag, "!PRODUCTCODE" and "!WEIGHT"
for($i=0;$i<$cnt;$i++){@$producttag=explode(";",$proverbs[$i]);
if(@$producttag[1]=="!PRODUCTCODE" && @$producttag[4]=="!WEIGHT")break;}

//-Extract all product data,and $a=$i+1 is where product starting to explode
//-after getting the product tag code and weight
for($a=$i+1;$a<$cnt;$a++){
$val=explode(";",$proverbs[$a]);
$str=$str.$val[1].";".$val[4].";"."\n";}

//result save in csv format with delimiter ";"
$str=$producttag[1].";".$producttag[4].";"."\n".$str;
file_put_contents("output.csv",$str);
echo $str;

?>

Open in new window

I am getting same kind of error:

Warning: file_put_contents(/output.csv): failed to open stream: Permission denied in /Users/abx/Sites/sandbox/test_weight2.php on line 23

this is line 23

file_put_contents("output.csv",$str);
the output is okay or not from echo $str on your server, Is it what you want ?

just make a  simple test first , could you create a file at your Linux shell and at such directory,
using command of linux such as "vim output.csv", to see it is permission issus or not
first ?

It might be disk full issue for your server account I guess if your input csv file  is really huge?
Try  "df -h" to see disk space avail

And what is weight_nsp_ben.csv,  input csv file size ?

I have no such issue  at my server running with your simple  weight_nsp_ben.csv  file posted
in this thread

Duncan
It appears that PHP does not have "write" permissions for the place your script tries to put the file.  I don't know much about Mac OSX, but I believe that it closely follows Unix file permissions.  You might want to skip the top of this page and go straight to "Permissions Defined" and read on from there to get some understanding of what you need to do.
http://support.apple.com/kb/ht2963
>>I even chmodded it to 755.
Be sure to adjust the permissions on the folder as well.
Just a thought... PHP has fputcsv() to help with this.  It handles delimiters and enclosure characters more-or-less automatically.
dear sharingsunshine,

Sorry I forgot to put the path in file_put_contents (for my server, it is no need)
probably it seems it is accessible file issue. I suggest

1-On top of my code please add it back,  
chdir(dirname(__FILE__)) since it reported Warning: file_put_contents(/output.csv)
in which that is no directory specified.  And be safe, also modified the code of
file_put_contents("output.csv",$str); to file_put_contents(dirname(__FILE__)."/".
"output.csv",$str);

2-Be reminded,
getchd() will return to the path of main script and
dirname(__FILE__) will return the path of the script currently running

3- Up to now it seems it is permission issue not path extraction issue
so please take a closer look at every directory or sub-directory permission
for the path of /Users/abx/Sites/sandbox/ by "ls -la" command ( or by MAC OS x command)  and look at the right of the directory , group & owner, if need, please change it back to your correct right group and owner by

chown -R -v myloginname *   //change ownership of file
chgrp -R -v myloginname *   //change ownership of group

Probably it is no issue for your system, just for cross-check only in cases

4- Please check  the right of  the your php script file,"test_weight2.php" ,
and correct back its group and owner if you FTP the file from other user account
to your server. If you can edit and save it at the same directoy, "/Users/abx/Sites/sandbox/",
there is impossilbe you can NOT  use file_put_contents to save result or output file if the path
is correct. So question, how you get the php script file on your server ,  from FTP or from server's editor ?

5-I am not quite sure about php permission on MAC system , please try to change
the file of php script, "test_weight2.php" to 777 by chmod -R  for testing to see
it work or not

you should NOT change the output file permission manually since it is impossible to do it
every time when you run your php script and it is not good practices



Hope it help on you to solve the issue finally

Duncan
<?php
chdir(dirname(__FILE__));
$proverbs=file("weight_nsp_ben.csv");$cnt=count($proverbs);$str="";

//-Finding product tag, "!PRODUCTCODE" and "!WEIGHT"
for($i=0;$i<$cnt;$i++){@$producttag=explode(";",$proverbs[$i]);
if(@$producttag[1]=="!PRODUCTCODE" && @$producttag[4]=="!WEIGHT")break;}

//-Extract all product data,and $a=$i+1 is where product starting to explode
//-after getting the product tag code and weight
for($a=$i+1;$a<$cnt;$a++){
$val=explode(";",$proverbs[$a]);
$str=$str.$val[1].";".$val[4].";"."\n";}

//result save in csv format with delimiter ";"
$str=$producttag[1].";".$producttag[4].";"."\n".$str;
file_put_contents(dirname(__FILE__)."/"."output.csv",$str);
echo $str;

?>

Open in new window

The input file is only 2 MB.  The variable $str is only writing two semi colons ;; to the output.csv file.

the permissions issues was fixed based on the link Ray gave me, the issue was you have to set the permissions on the directory first then apply to all enclosed items.

I still need the file to be created with productcode and weight for each item.  

Thanks,

Randal
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that is great and does the job.  However, it does show this error after execution

Notice: Undefined offset: 1 in /Users/rjw/Sites/sandbox/test_weight2.php on line 35

Notice: Undefined offset: 4 in /Users/rjw/Sites/sandbox/test_weight2.php on line 35

your line 35 is
 $dat = array($row[$code], $row[$wght]);

If you can explain that it would be great but not mandatory.  Thanks for your help and also telling me how to get the permissions fixed permanently.
I think the problem may occur because there is an extra end-of-line character after the last line.  You might add a test to see if the $row variable is empty() and skip those lines.

Thanks for the points and best of luck with your project, ~Ray
Your welcome on the points and thanks for the additional tip on testing for empty.