ballhogjoni Posted May 19, 2013 Share Posted May 19, 2013 Is there a php class to update a csv file? I'm not very well versed with fputcsv, but looking at the documentation it doesn't seem to give me what I want. I see that I pass it the file handler and an array of data to write to the csv. I want to update one column of data at a time not an entire row. I'm stuck on where to go from here. In one csv I have a product sku that is located in column 9 and the other csv column 13. These columns are the identifying info I need to know I am looking at the same product on both csv's. The "In" csv is the origin which contains the inventory quantity. I need to transfer that quantity to the "out" csv. I figure the way to do this is by reading the origin line by line and placing the sku and the inventory in an array. I then loop through that array and read the "out" csv and search for a match. Once I find a match I need to update that products' inventory column from the origin csv. Makes sense? Any help would be helpful. This is the code I have so far, probably far off from what I need. <?php error_reporting(E_ALL); ini_set('display_errors', '1'); ini_set('auto_detect_line_endings', true); if (empty($_GET['in'])) die("Please pass file name to check inventory."); if (empty($_GET['out'])) die("Please pass file name to update inventory."); $in = $_GET['in'] . ".csv"; $out = $_GET['out'] . ".csv"; $h_in = fopen($in, "r"); if (!$h_in) die("$in can't be read."); $h_out = fopen($out, "r+"); if (!$h_out) die("$out can't be opened to read and write"); $rsr_inv = array(); $productskus = array(); $products = array(); $flag = true; while (false !== ($row = fgetcsv($h_out))) { if($flag) { $flag = false; continue; } $productskus[] = $row[13]; $products[] = $row; } while (false !== ($row = fgetcsv($h_in))) { $key = array_search($row[0], $productskus); if ($key) { $rsr_inv[] = array($productskus[$key], $row[8]); } } fclose($h_in); foreach($products as $key => $prod){ echo $inv[0] . ":" . $products[$key][13] . "<br>"; //if($inv[0] == $products[$key][13]){} } fclose($h_out); Quote Link to comment https://forums.phpfreaks.com/topic/278189-csv-class-need-help-updating-a-csv-file/ Share on other sites More sharing options...
mac_gyver Posted May 20, 2013 Share Posted May 20, 2013 here is how i would do this - 1) open and read all the "in" data into an array with sky as the index/key and the quantity is the array value. close the file as you no longer need the file handle. 2) open the existing "out" file for reading and open a temporary file for writing. 3) in a loop, use fgetcsv to read each line from the existing "out" file into an array, one line at a time. using the sku element of this array, get the quantity from the array made in step #1 and store it to the correct quantity element of the current line's array. write the array to the temporary output file made in step #2. 4) when done looping through all the lines in the existing "out" file, close all open files and if there have been no errors, delete the original "out" file and rename the temporary file to the original "out" file name. Quote Link to comment https://forums.phpfreaks.com/topic/278189-csv-class-need-help-updating-a-csv-file/#findComment-1431086 Share on other sites More sharing options...
Solution mac_gyver Posted May 20, 2013 Solution Share Posted May 20, 2013 the basic logic would be - <?php $infile = 'in.csv'; // a file for testing only $csvfile = 'out.csv'; // another file for testing only $tempfile = tempnam(".", "tmp"); // produce a temporary file name, in the current directory // modify the values to match your data structure define('IN_SKU',1); // define the offset of the incoming sku column define('IN_QTY',2); // define the offset of the incoming quantity column define('OUT_SKU',1); // define the offset of the outgoing sku column define('OUT_QTY',2); // define the offset of the outgoing quantity column // read incoming sku/qty if(!$handle = fopen($infile, "r")){ die('could not open incoming file'); } $in_data = array(); $header = true; while(($data = fgetcsv($handle)) !== FALSE){ if($header){ $header = false; continue; } $in_data[$data[IN_SKU]] = $data[IN_QTY]; } fclose($handle); // process data, updating the quantity with any incoming quantity if(!$input = fopen($csvfile,'r')){ die('could not open existing csv file'); } if(!$output = fopen($tempfile,'w')){ die('could not open temporary output file'); } $header = true; while(($data = fgetcsv($input)) !== FALSE){ if(!$header && isset($in_data[$data[OUT_SKU]])){ $data[OUT_QTY] = $in_data[$data[OUT_SKU]]; // modify qty if exists in the in_data } fputcsv($output,$data); // write the line to the output $header = false; } fclose($input); fclose($output); unlink($csvfile); rename($tempfile,$csvfile); echo 'done'; Quote Link to comment https://forums.phpfreaks.com/topic/278189-csv-class-need-help-updating-a-csv-file/#findComment-1431087 Share on other sites More sharing options...
ballhogjoni Posted May 20, 2013 Author Share Posted May 20, 2013 You are macgyver! Awesome thanks, this is what I need! Quote Link to comment https://forums.phpfreaks.com/topic/278189-csv-class-need-help-updating-a-csv-file/#findComment-1431104 Share on other sites More sharing options...
ignace Posted May 20, 2013 Share Posted May 20, 2013 Is there a php class to update a csv file? SplFileObject combines all file operations. Quote Link to comment https://forums.phpfreaks.com/topic/278189-csv-class-need-help-updating-a-csv-file/#findComment-1431134 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.