Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

CSV Class? Need help updating a csv file.

$
0
0

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);

Viewing all articles
Browse latest Browse all 13200

Trending Articles