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 magento
at 2024-03-20 22:34:04
Point:500 Replies:16 POST_ID:828899USER_ID:11456
Topic:
PHP Scripting Language;;MySQL Server
Hi,
Have a csv file, need the output as below:
Input:
ID Product Details
2500 PROD894,PROD888,PROD900,PROD23,PROD17
2501 PROD390
2502 PROD388
2503 PROD440,PROD433,PROD475
Output:
ID Product Details
2500 PROD894
2500 PROD888
2500 PROD900
2500 PROD23
2500 PROD17
2501 PROD390
2502 PROD388
2503 PROD440
2503 PROD443
2503 PROD475
Thanks
Have a csv file, need the output as below:
Input:
ID Product Details
2500 PROD894,PROD888,PROD900,PROD23,PROD17
2501 PROD390
2502 PROD388
2503 PROD440,PROD433,PROD475
Output:
ID Product Details
2500 PROD894
2500 PROD888
2500 PROD900
2500 PROD23
2500 PROD17
2501 PROD390
2502 PROD388
2503 PROD440
2503 PROD443
2503 PROD475
Thanks
Author: magento replied at 2024-05-26 04:12:52
Thanks for all ur kind help and apologies for the delay.
Author: magento replied at 2024-03-22 10:04:41
Hi All,
First i apologies to everybody, still i am facing issue with my Xampp.
I dont know why the apache is not starting and only mysql is running in xampp admin.
Once rectified, i will check every one of the code and provide update.
Thanks,
First i apologies to everybody, still i am facing issue with my Xampp.
I dont know why the apache is not starting and only mysql is running in xampp admin.
Once rectified, i will check every one of the code and provide update.
Thanks,
Accepted Solution
Expert: Ray Paseur replied at 2024-03-21 07:08:15
125 points EXCELLENT
This appears to work OK.
http://iconoun.com/demo/temp_magento.php
Input file:
http://iconoun.com/demo/temp_magento.csv
Output file:
http://iconoun.com/demo/temp_magento.txt
Script file:
http://iconoun.com/demo/temp_magento.php
Input file:
http://iconoun.com/demo/temp_magento.csv
Output file:
http://iconoun.com/demo/temp_magento.txt
Script file:
<?php //demo/temp_magento.phperror_reporting(E_ALL);// WHERE TO SAVE THE OUTPUT$new = 'temp_magento.txt';// OPEN THE CSV FILE$url = 'temp_magento.csv';$fpr = fopen($url, 'r');if (!$fpr) trigger_error("UNABLE TO OPEN $url", E_USER_ERROR);// PREPARE AN OUTPUT DOCUMENT$out = array();// READ RECORDS FROM THE CSV FILEwhile (!feof($fpr)){ $csv = fgetcsv($fpr); // NORMALIZE THE SECOND POSITION TO TRIM BLANKS AND EXTRANEOUS COMMAS $tmp = $csv[1]; $tmp = trim($tmp); $tmp = trim($tmp, ','); // IF THE CSV DATA CONTAINS EMBEDDED COMMAS if (strpos($tmp, ',')) { // EXPLODE THE DATA INTO AN ARRAY $tmp = explode(',', $tmp); } // IF NO EMBEDDED COMMAS else { // MAKE AN ARRAY WITH ONE DATA ELEMENT $tmp = array($tmp); } // USING THE ARRAY DATA, CREATE ONE LINE OF OUTPUT FROM EACH ELEMENT OF THE ARRAY foreach ($tmp as $str) { $out[] = $csv[0] . ' ' . $str . PHP_EOL; }}// WRITE THE OUTPUT DOCUMENTfile_put_contents($new, $out); 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:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:
HTH, ~Ray
Expert: rinfo replied at 2024-03-21 03:59:06
this is my simple and crude code to achieve what you are trying to achieve.
Expert: insoftservice replied at 2024-03-21 01:40:35
@magento please revert whether given code is working as expected or not
Expert: duncanb7 replied at 2024-03-21 00:40:21
Add back the first line of "ID PRODUCT Detail"
You input file format is different on this thread compared to the test.csv, that is
no "PROD", but it will be easier and simple
Please read the attached file
input.csv format is changed from input.csv to test.csv as you request
junk.php is changed to junk2.php
Be reminded your product detail data with " quote at start and end of second column string such as "0,0,0,0"
Duncan
You input file format is different on this thread compared to the test.csv, that is
no "PROD", but it will be easier and simple
Please read the attached file
input.csv format is changed from input.csv to test.csv as you request
junk.php is changed to junk2.php
Be reminded your product detail data with " quote at start and end of second column string such as "0,0,0,0"
Duncan
<?php$proverbs=file("test.csv");$cnt=count($proverbs);$kline=array();array_push($kline,$proverbs[0]);for ($i=1;$i<$cnt;$i++){$mline=explode(",",$proverbs[$i]);$cnt_2=count($mline);for($a=1;$a<$cnt_2;$a++){array_push($kline,$mline[0].",".str_replace('"',"",trim($mline[$a],""))."");}}$cnt=count($kline);file_put_contents("output.csv",$kline); ?> 1:2:3:4:5:6:7:8:9:10:
Expert: insoftservice replied at 2024-03-21 00:16:41
Sorry forgot to paste the o/p.
$datadump[$idval][]= trim(trim($dataval),"");
line 26 is not required. You can run without that too.
$datadump[$idval][]= trim(trim($dataval),"");
line 26 is not required. You can run without that too.
Expert: Amar Bardoliwala replied at 2024-03-21 00:15:28
Hello magento,
did u try my code?
It should work fine for u. you will see expected output in browser window.
Also you should put your test.csv file in same directory as your php file.
How are you running the code? in browser or from command line?
are you getting any error while running the code?
Thank you.
Amar Bardoliwala
did u try my code?
It should work fine for u. you will see expected output in browser window.
Also you should put your test.csv file in same directory as your php file.
How are you running the code? in browser or from command line?
are you getting any error while running the code?
Thank you.
Amar Bardoliwala
Assisted Solution
Expert: insoftservice replied at 2024-03-21 00:14:58
125 points EXCELLENT
Please try this one
<?php$filename= "test.csv";$val = file($filename);$datadumper[] = "ID,Product Details";if(!empty($val)){ foreach($val as $key=>$value) { $csvval = explode(',',trim($value,""));// echo '<pre>';print_r($csvval); if(($csvval[0]=="ID") || ($csvval[0] =='Product Details')) { continue; } else { $idval = $csvval[0]; unset($csvval[0]); $dataval = $csvval; if(is_array($dataval)) { foreach($dataval as $datakey=>$dataval) { $dataval = str_replace('"'," ",$dataval); $dataprint= trim(trim($dataval),""); $datadump[$idval][]= trim(trim($dataval),""); $datadumper[] = "$idval,$dataprint"; } }else { $dump = trim($csvval,'"'); $dataprints= trim(trim($dump),""); $datadump[$idval][] = $dump; $datadumper[] = "$idval,$dataprints"; } } }}echo "<pre>";print_r($datadumper);file_put_contents("outputs.csv",$datadumper);?> 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:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:
Author: magento replied at 2024-03-21 00:04:20
@magento please revert whether given code is working as expected or not
Assisted Solution
Expert: duncanb7 replied at 2024-03-20 23:38:15
125 points EXCELLENT
in my code, I already take care of delimiter of comma "," as follows
$mline=explode(",",$proverbs[$i]);
I mean just make sure the char between 25xx and PRODxxx that is space rather
than other character for doing math comparsion.
I tested the code it works and output the exact what you want with just simple code
please see all attached file
Duncan
$mline=explode(",",$proverbs[$i]);
I mean just make sure the char between 25xx and PRODxxx that is space rather
than other character for doing math comparsion.
I tested the code it works and output the exact what you want with just simple code
please see all attached file
Duncan
Author: magento replied at 2024-03-20 23:33:01
Hi Duncab7,
Its a csv file so the delimiter is comma.
I have attached the csv file to previous post .
Not sure, my xampp was not running properly and hence i cant test it. Ca you pls run it and send me the output file. That would be great !!!
Thanks
Its a csv file so the delimiter is comma.
I have attached the csv file to previous post .
Not sure, my xampp was not running properly and hence i cant test it. Ca you pls run it and send me the output file. That would be great !!!
Thanks
Assisted Solution
Expert: Amar Bardoliwala replied at 2024-03-20 23:31:39
125 points EXCELLENT
Hello magento,
Please check with code below
Please check with code below
<?php$file = fopen("test.csv", "r");$members = array();$x = 0;while (!feof($file)) { if($x > 0) { $arr_line = fgetcsv($file); $arr_prod = explode(',',$arr_line[1]); foreach($arr_prod as $key => $val) { if(trim($val) != '' && trim($arr_line[0]) != '') { echo $arr_line[0] . " " . $val . "<br />"; } } } else { $line = fgets($file); echo "ID Product Details<br/>"; } $x++;}fclose($file);?> 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:31:
Hope this will help you.
Thank you.
Amar Bardoliwala
Expert: duncanb7 replied at 2024-03-20 23:25:30
Dear magento,
you can try following code and I tested it works , extraction is starting at after line of "ID Product Details" , and I suppose the character between "2500" and "PRODXXX" is space, otherwise you need to find out the exact delimiter for explode() function in php if it is not a space between them
Duncan
you can try following code and I tested it works , extraction is starting at after line of "ID Product Details" , and I suppose the character between "2500" and "PRODXXX" is space, otherwise you need to find out the exact delimiter for explode() function in php if it is not a space between them
Duncan
<?php$proverbs=file("input.csv");$cnt=count($proverbs);$kline=array();for ($i=1;$i<$cnt;$i++){$mline=explode(",",$proverbs[$i]);$cnt_2=count($mline);for($a=0;$a<$cnt_2;$a++){array_push($kline,trim($mline[$a],"")."");}}$cnt=count($kline);for ($a=0;$a<$cnt-1;$a++){$mline=explode(" ",$kline[$a]);$next=explode(" ",$kline[$a+1]);//echo $next[0]."=$a=".$mline[0]."===";if ($next[0]>0){} else {$kline[$a+1]=$mline[0]." ".$kline[$a+1];}}file_put_contents("output.csv",$kline);?> 1:2:3:4:5:6:7:8:9:10:11:12:
Author: magento replied at 2024-03-20 23:13:50
Amar,
I tried the above but for some reason i didnt get the output.
This is the input file i have tried, can you please run and send me the output?
Thanks
I tried the above but for some reason i didnt get the output.
This is the input file i have tried, can you please run and send me the output?
Thanks
Expert: Amar Bardoliwala replied at 2024-03-20 23:04:24
Hello magento,
considering that your input file is as above INPUT format and its name is text.csv.
you can try something like following
considering that your input file is as above INPUT format and its name is text.csv.
you can try something like following
<?php$file = fopen("test.csv", "r");$members = array();$x = 0;while (!feof($file)) { if($x > 0) { $line = fgets($file); $line = preg_replace('!s+!', ' ', $line); $arr_line = explode(' ', $line); $arr_prod = explode(',',$arr_line[1]); foreach($arr_prod as $key => $val) { if(trim($val) != '' && trim($arr_line[0]) != '') { echo $arr_line[0] . " " . $val . "<br />"; } } } else { $line = fgets($file); echo "ID Product Details<br/>"; } $x++;}fclose($file);?> 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:31:32:33:34:35:
Hope this will help you.
Thank you.
Amar Bardoliwala