Jump to content

Quick help needed


Schlo_50

Recommended Posts

I am trying to access a file and replace the eighth key within it with a new string. The structure of the file is:

 

stringa|stringb|stringc|stringd|stringe|stringf|stringg|old one|

 

I want to open the file, find the last key 'old one' and replace it with a new string. My code so far is:

 

	$file = file("data/customers.DAT");
foreach($file as $Key => $Val){
$Data[$Key] = explode("|", $Val);

$status = trim($Data[$Key][8]);

}

 

I think I need some sort of fopen() but im not sure how to overwrite etc..

 

Any help appreciated!

Thanks

Link to comment
Share on other sites

<?php
$file = "data/customers.DAT";
$fh = fopen($file,'w');
foreach(file($file) as $Key => $Val){
  $parts = explode("|", trim($Val));
  $status = trim($parts[8]);
  $parts[8] = 'New String';
  fwrite($fh,implode("|",$parts)."\n");
}
fclose($fh);
?>

Link to comment
Share on other sites

Oops, sorry, need to do the file() before the fopen()

 

<?php
$file = "data/customers.DAT";
$lines = file($file);
$fh = fopen($file,'w');
foreach($lines as $Key => $Val){
  $parts = explode("|", trim($Val));
  $status = trim($parts[8]);
  $parts[8] = 'New String';
  fwrite($fh,implode("|",$parts)."\n");
}
fclose($fh);
?>

Link to comment
Share on other sites

I'd change rhodesa code to:

$file = 'data/customers.DAT';
$new_data = '';
foreach(file($file) as $Key => $Val)
{
    $parts = explode("|", trim($Val));
    $status = trim($parts[8]);
    $parts[8] = 'New String';

    // concate each line to $new_data string.
    $new_data .= implode('|', $parts) . PHP_EOL;
}

echo '<b>Data to be written to customers.DATA:<b><pre>' . $new_data . '</pre>';

// we can then clear the contents of the customers.DAT file and rewrite the new contents:
// open the file for writing (this will clear the contents of customers.DAT)
$fh = fopen($file,'w');

// write new data to customers.DAT
fwrite($fh, $new_data);

// close file
fclose($fh);

Link to comment
Share on other sites

You're still using the 'w' flag to write to the file. This causes an overwrite, and I believe you should be using the 'a' flag for append instead. However you'll need to seek to the previous line before appending, and this should overwrite the last line of the file, which is, i believe, what you're wanting. You want to alter the previous recored don't you, not all of them?

Link to comment
Share on other sites

The 'w' does clear out the file, which is fine, you just need to make sure you read the current contents into a variable first. Opening it in append mode and trying to move the cursor around seems like an un-needed hassle. The second post I made should work.

 

Oops, sorry, need to do the file() before the fopen()

 

<?php
$file = "data/customers.DAT";
$lines = file($file);
$fh = fopen($file,'w');
foreach($lines as $Key => $Val){
  $parts = explode("|", trim($Val));
  $status = trim($parts[8]);
  $parts[8] = 'New String';
  fwrite($fh,implode("|",$parts)."\n");
}
fclose($fh);
?>

Link to comment
Share on other sites

A smart way to do this is to fwrite to a new temp file.

 

Ex file.txt.new

 

Once the file is written successfully, rename (and overwrite) the old file to file.txt.old and the new one to file.txt. Redundancy! If anything breaks, you have a quick reference point to recover to. Repeat

Link to comment
Share on other sites

I am using the code you posted rhodesa, but im having trouble. The code seems to remove all lines of data apart from one.

 

I have multiple lines within the file. The code needs to search through the file for a match and then edit the last key according to the string specified. The whole code is below:

 

$file = "data/customers.DAT";
$lines = file($file);
$fh = fopen($file,'w');
foreach($lines as $Key => $Val){
  $parts = explode("|", $Val);
  $status = $parts[8];
  if ($_GET['lookup'] == $parts[0]) {

  $parts[8] = "".$_POST['status']."";
  fwrite($fh,implode("|",$parts)."\n");
}
}
fclose($fh);

 

Thanks

Link to comment
Share on other sites

That is because the had the fwrite inside the IF statement. This should work for you:

 

<?php
  $file = "data/customers.DAT";
  $lines = file($file);
  $fh = fopen($file,'w');
  foreach($lines as $Key => $Val){
    $parts = explode("|", $Val);
    $status = $parts[8];
    if ($_GET['lookup'] == $parts[0]) {
      $parts[8] = $_POST['status'];
    }
    fwrite($fh,implode("|",$parts)."\n");
  }
  fclose($fh);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.