Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by sharingsunshine
at 2024-01-04 17:13:02
Point:500 Replies:14 POST_ID:828870USER_ID:11779
Topic:
Mac OS X;;PHP Scripting Language
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
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;!MANUFACTURER889;"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"; 1:2:3:4:5:6:7:8:9:
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);"?>" 1:2:3:4:5:6:7:8:
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
Author: sharingsunshine replied at 2024-01-06 13:27:56
Your welcome on the points and thanks for the additional tip on testing for empty.
Expert: Ray Paseur replied at 2024-01-06 11:57:48
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
Thanks for the points and best of luck with your project, ~Ray
Author: sharingsunshine replied at 2024-01-06 08:33:49
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.
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.
Accepted Solution
Expert: Ray Paseur replied at 2024-01-06 06:59:26
500 points EXCELLENT
This seems to test out correctly.
<?php // RAY_fgetcsv.phperror_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 TIMEwhile (!feof($fpr)){ $row = fgetcsv($fpr, 8192, ";"); $dat = array($row[$code], $row[$wght]); $out = fputcsv($fpw, $dat);} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:
Contents of the output CSV at http://www.laprbass.com/RAY_temp_sharingsunshine_new.csv
Author: sharingsunshine replied at 2024-01-06 04:58:32
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
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
Expert: duncanb7 replied at 2024-01-05 14:39:27
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
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
<?phpchdir(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 weightfor($a=$i+1;$a<$cnt;$a++){$val=explode(";",$proverbs[$a]);$str=$str.$val[1].";".$val[4].";"."";}//result save in csv format with delimiter ";"$str=$producttag[1].";".$producttag[4].";"."".$str;file_put_contents(dirname(__FILE__)."/"."output.csv",$str);echo $str;?> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:
Expert: Ray Paseur replied at 2024-01-05 10:35:58
Just a thought... PHP has fputcsv() to help with this. It handles delimiters and enclosure characters more-or-less automatically.
Expert: hielo replied at 2024-01-05 09:43:08
>>I even chmodded it to 755.
Be sure to adjust the permissions on the folder as well.
Be sure to adjust the permissions on the folder as well.
Expert: Ray Paseur replied at 2024-01-05 03:17:14
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
http://support.apple.com/kb/ht2963
Expert: duncanb7 replied at 2024-01-04 21:59:28
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
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
Author: sharingsunshine replied at 2024-01-04 21:38:39
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);
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);
Expert: duncanb7 replied at 2024-01-04 20:55:32
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
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 weightfor($a=$i+1;$a<$cnt;$a++){$val=explode(";",$proverbs[$a]);$str=$str.$val[1].";".$val[4].";"."";}//result save in csv format with delimiter ";"$str=$producttag[1].";".$producttag[4].";"."".$str;file_put_contents("output.csv",$str);echo $str;?> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:
Author: sharingsunshine replied at 2024-01-04 18:46:56
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.
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.
Expert: hielo replied at 2024-01-04 17:41:22
<?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) );}?> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23: