Link to home
Create AccountLog in
Avatar of tomrector
tomrectorFlag for United States of America

asked on

Edit Flatfile with PHP

I want to use php to replace data in a flatfile. (sorry about the format below, just paste to notepad for clarity):
my flat file is foramted as a CSV with the 'pipe' as the delimiter instead of a comma
each row is a 'record' with unique record numbers, "ID"
the rowdata is identified by column names at the top.  Total file is 1600 lines long.

|ID|Item|Qty|Cate|Price4|Price1|Price5|Price6|Price7|Out|Due|Name|Total|avail|image|safety|
|2101|12 ft Utility Trailer|1|Trailer||30.00 |75.00 |45.00 |150.00 |||||Yes|Image2101.png|2101_S.pdf|
|2102|16 ft Utility Trailer|1|Trailer||35.00 |87.50 |52.50 |175.00 |||||Yes|Image2102.png|2101_S.pdf|
|2103|2 Person Tent|1|Camping||6.00 |15.00 |9.00 |30.00 |||||Yes|Image2103.png||
|2104|2 Person Tent|1|Camping||6.00 |15.00 |9.00 |30.00 |||||Yes|Image2104.png||
|2105|24ft -29ft Travel Trailer|1|Camper||70.00 |175.00 |105.00 |350.00 |||||Yes|Image2105.png|2105_S.pdf|

etc...

I want to update the the 'avail' colmun  with "NO", the 'Name' column with a "name" and the Out and Due cols with a date
 (the 'change' data is gathered from a form passed to the script by using unique record number 'ID').

I have used the following in the past, but does not work withg my hosting provider now.

$arg = "http://www.mysite.com/cgi-bin/RentUpdate1.pl?ID=$ID_1&action=save&password=''&avail=No&Name=$LName&Due=$DueItemA";
require ($arg);

 I would appreciate any direction, I prefer to do it from same php script.
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

<?php
// The filenames we'll be dealing with
$inFile = "original.csv";
$outFile = "updated.csv";

// Open the file handles - read the original and write the updated file
$fpIn = fopen($inFile,"r");
$fpOut = fopen($outFile,"w");

// Read the header line and copy it to the new file
$header = fgetcsv($fpIn,0,'|');
fputcsv($fpOut,$header,'|');

// Find the index of the avail column
$idxAvail = array_search("avail",$header);

// Read all the records
while(!feof($fpIn))
{
  // Read in a pipe-delimited record into an array
  $record = fgetcsv($fpIn,0,'|');

  // Update the "avail" column to "No" for item ID 2103
  if($record[0] == "2103")
  {
    $record[$idxAvail] = "No";
  }

  // Write the updated record out to the new file
  fputcsv($fpOut,$record,'|');
}

// Close the file handles
fclose($fpIn);
fclose($fpOut);
?>

Open in new window

... but does not work with my hosting provider...
How does this not work?  Does it cause a fire, or make the machine tip over, or scares the cat?  Or maybe it creates an error message of some sort?  If there is a message, we might be able to help with that part ;-)
Avatar of tomrector

ASKER

gr8gonzo,  thanks for your support,,,,
 error:
Warning: fputcsv() expects parameter 2 to be array, boolean given in /hermes/waloraweb096/b500/moo.thrsevfi/misc/edit.php on line 30


Ray
    for the php 'require', The hosting provifder I use now has 'require'  disabled at server level,  adding 'allow_url_fopen = ON ' to php.ini does not change anything
Your hosting provider has disabled "require" ???? Are you 100% certain about that? I could understand if they disabled url_fopen, but I've NEVER seen a provider disable something as essential as require, especially since it's not a risk of any sort.

If the only problem is connectivity via the URL wrapper, you can always hit the URL with cURL...

If you're getting a boolean for the second parameter, then it sounds like fgetcsv is not returning the expected data. You could also try changing this out:

Old:
// Read all the records
while(!feof($fpIn))
{
  // Read in a pipe-delimited record into an array
  $record = fgetcsv($fpIn,0,'|');

  // Update the "avail" column to "No" for item ID 2103
  if($record[0] == "2103")
  {
    $record[$idxAvail] = "No";
  }

  // Write the updated record out to the new file
  fputcsv($fpOut,$record,'|');
}

Open in new window

New:
// Read in a pipe-delimited record into an array
while (($record = fgetcsv($fpIn, 0, '|')) !== false) 
{
  // Update the "avail" column to "No" for item ID 2103
  if($record[0] == "2103")
  {
    $record[$idxAvail] = "No";
  }

  // Write the updated record out to the new file
  fputcsv($fpOut,$record,'|');
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account