Solved

Need To Parse CSV file in PHP

Posted on 2014-01-04
14
812 Views
Last Modified: 2014-01-15
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
0
Comment
Question by:sharingsunshine
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 82

Expert Comment

by:hielo
Comment Utility
<?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

0
 

Author Comment

by:sharingsunshine
Comment Utility
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.
0
 
LVL 13

Expert Comment

by:duncanb7
Comment Utility
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

0
 

Author Comment

by:sharingsunshine
Comment Utility
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);
0
 
LVL 13

Expert Comment

by:duncanb7
Comment Utility
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
>>I even chmodded it to 755.
Be sure to adjust the permissions on the folder as well.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Just a thought... PHP has fputcsv() to help with this.  It handles delimiters and enclosure characters more-or-less automatically.
0
 
LVL 13

Expert Comment

by:duncanb7
Comment Utility
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

0
 

Author Comment

by:sharingsunshine
Comment Utility
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
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
This seems to test out correctly.

<?php // RAY_fgetcsv.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Apple/Operating_Systems/OS_X/Q_28330742.html

// NAME OF THE INPUT AND OUTPUT CSV FILE
$csv = 'RAY_temp_sharingsunshine.csv';
$out = 'RAY_temp_sharingsunshine_new.csv';

// OPEN THE INPUT AND DISCARD THE TOP ROW
$fpr = fopen($csv, 'r');
if (!$fpr) trigger_error("CANNOT READ $csv", E_USER_ERROR);
fgets($fpr);

// OPEN THE OUTPUT
$fpw = fopen($out, 'w');
if (!$fpw) trigger_error("CANNOT WRITE $out", E_USER_ERROR);

// GET THE COLUMN NAMES AND LOCATE THE COLUMN OFFSETS WE WANT
$cols = fgetcsv($fpr, 8192, ';');
$code = array_search('!PRODUCTCODE', $cols);
$wght = array_search('!WEIGHT',      $cols);

// READ AND WRITE ONE ROW AT A TIME
while (!feof($fpr))
{
    $row = fgetcsv($fpr, 8192, ";");
    $dat = array($row[$code], $row[$wght]);
    $out = fputcsv($fpw, $dat);
}

Open in new window

Contents of the output CSV at http://www.laprbass.com/RAY_temp_sharingsunshine_new.csv
10810-7,0
14553-4,0
150-6,0
16343-9,0
166-7,0
1672-1,0
1958-8,0

Open in new window

0
 

Author Closing Comment

by:sharingsunshine
Comment Utility
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
0
 

Author Comment

by:sharingsunshine
Comment Utility
Your welcome on the points and thanks for the additional tip on testing for empty.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now