Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit row in flat file

Status
Not open for further replies.

buzzt

Programmer
Oct 17, 2002
171
CA
I am able to retrieve the row I want (from a flat file). Now I would like to be able to edit and/ or delete that row only without affecting those before or after. Is there a way?

Here's how I am getting the row info ($ed is the id for that row which is posted to the page by clicking a link with the id for that row in it.)

Code:
   $file  = fopen("ans.txt","a+");
   $count = file("ans.txt");
   $num   = count($count);

   for ($i=0; $i<$num; $i++)
   {
     $edrow = explode("|", $count[$i]);
     if ($edrow[0]==$ed)
     {
     	$edid   = $edrow[0];
     	$edqrow = $edrow[1];
     	$edarow = $edrow[2];
     }
     
   }
 
Here's one reason why flat files are no fun.

You can't really do what you want. You need to read in the entire file, make your changes, and rewrite the entire file.

You can append to the end of a flat file, but you can not insert in the middle or modify the file.

 
Any idea how I would rewrite it from that point on?
 
Can't do that either.

For small files I know will stay small I just read the whole thing in using file()... then work on the array, then truncate the file and rewrite the whole thing.

For larger files, I read in the stream in manageable chunks, make my changes to the stream, write it out to a temporary file, delete the original, rename the temporary.

When the files are larger I do everything possible to use a database.
 
Oh well... I can just edit the text file manually then. Thanks.
 
You can do it the way you do.
You have already read the whole file into an array.
You have identified the correct row by iterating the array.
All you need next is to replace the array element identified and then write the array to the file.
 
So here's my form. What am I doing wrong?
Code:
<?
if ($ed != "")
{
for ($i=0; $i<$num; $i++)
{
$edrow = explode("|", $count[$i]);
if ($edrow[0]==$ed)
{
$edid   = $edrow[0];
$edqrow = $edrow[1];
$edarow = $edrow[2];
}

}

?>
<form name="ed" method="post" action="add.php">
<input name="edid" type="text" id="addq" size="3" value="<?=$edid?>">
<br>
<br>
<input name="edq" type="text" id="addq" size="75" value="<?=$edqrow?>">
<br>
<br>
<textarea name="eda" cols="69" rows="4" id="adda"><?=$edarow?></textarea>
<br>
<input type="submit" name="Submit" value="Submit">&nbsp;&nbsp;<input type="reset" name="Submit" value="Reset">
</form>
<?

array_splice($edrow, 1, array($edid,$edq,$eda));

}



?>
 
Help me out a bit...

You have a text file with many rows. Each row has 3 fields separated by |

Now, you want to present a form which accepts an ID, a something else called edqrow, and something else which is called edawrow.

Then, what's your goal?

As I see it your code is using unitialized values and the like which could definitely be causing you problems.

 
Check your register_globals settings. It is probably OFF and should be.
You need to refer to the posted values as $_POST['ed'] etc.
 
On submit;

$edid replaced by $edid //I should have named this differently
$edqrow replaced by $edq
$edarow replaced by $eda

in array $edrow
 
ok.. so find the matching ID and replace the q & a values is what you mean?

Here's the meat of how I'd do it.

Code:
<?php
$filename="something.txt"; 

if (isset($_POST['edid'])) {  //obviouslly don't do it if they didn't fill out the form
  $set = false;  //to keep track of if we made a change
  $rows = file($filename);  //read the file into an array
  
  foreach ($rows as $row_num=>$row) { //loop through the array
    $values=split('|', $row);  
    if ($values[0] == $_POST['edid']) {  
      $new_row = $_POST['edid'].'|'.$_POST['edqrow'].'|'.$_POST['edarow'];
      $rows[$row_num]=$new_row;  //format new row
      $set = true;  
      break;  //stop looking we found it anyway (optional)
    }
  }
  if ($set) {  //why bother if we made no changes?
    $contents=implode("\n", $rows);  //turn the array back into one long string... depending on OS \n may need some tweaking
    $fh = fopen($filename, 'w');  //open the file in a truncated mode
    fwrite($fh, $contents);  //write the contents out
    fclose($fh);  //always close your file handles!
    echo 'File modified';  //tell use we did something
  }
  else {
    echo 'File ID not located, no changes made.';  //tell us the id was bad
  }
}
//your form stuff here

Sorry, I usually prefer to offer the thought process instead of the code... but in this case at this time this is the best I can do.
 
Close... but it doesn't take the place of the existing row. It re-writes the first row. Also, even though is rewrites the first row I always get this error:

Warning: split(): REG_EMPTY in FOLDER LOCATION on line 83
 
Works! Finally... I'm just curious though, the array_splice() seemed quite promising. Could I have accomplished the same with it?
 
Sure... array splice does the same thing with different arguments. If you would've known the offset it would make more sense to use it. Or if you were using text based keys, and wanted to change the key at a particular offset.

It's also great if you're replacing a range of your array with a different sized range... say you wanted to find the key and put three entries in place of it and you wanted them all close to eachother.

IMHO, it's overly complex for this problem though.
 
All right, there's one argument for my suggestion of the overly complicated solution:

I suggested array_splice() because it will easily remove or replace a row in the array. The given solution (if I read it correctly) does not accomodate for the deletion of a row.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top