Question

Would appreciate some assistance

i have a txt file witht he following contents:

1234|dog|apartment|two
1234|cat|apartment|one
1234|dog|house|two
1234|dog|apartment|three

I want to delete the entry where the animal is "dog" living in an "house"

<?php
if (isset($_POST['delete_entry]))
{
    //identifies the file
    $file = "db.txt";
    //opens the file to read
    @$fpo = fopen($file, 'r');
    //while we have not reached the end of the file
    while(!feof($fpo))
    {
        //read each line of the file into an array called animal 
        $animal[] = fgets($fpo);
    }
    //close the file
    fclose($fpo);

    //iterate through the array
    foreach ($animal as $a)
    {
        if the string contains dog and apartment
        if ((stripos ($a, 'dog']))&&(stripos ($a, 'house')))
        {
            //dont do anything            
        }
        else
        {
            //otherwise print out the string
            echo $a.'<br/>';
        }
    }
}
?>

This successfully prints out the array without the entry where 'dog' and 'house' appears. I need to write this back to the flat file though, but running into difficulties.

I have tried a variety of options include writting back to the file immediately when each entry is found.

Warning: feof() expects parameter 1 to be resource, boolean given in 
Warning: fwrite(): 9 is not a valid stream resource in
Warning: fclose(): 9 is not a valid stream resource in 

These are amongst the errors i have encountered. Now from my understanding of arrays,
- when i go through this array called animal,
- it checks index [0] for the two conditions and
- if the entry is not found, it assigns to to $a.
- It then goes through the array starting at index [1],
- and so forth.
Each time the new value is assigned to $a.

I thought that printing it to file each time it appears might work, but this is where i get the fwrite and fclose errors above, and no idea how to resolve this (yet).

I still have to do the bit where i need to replace 'apartment' with house, for one specifically selected entry, but will get there once I have sorted out the "delete"

I dont need code, maybe just a logic flow that might assist me.

Thanks

Was it helpful?

Solution

To save some time, you could store your data in array only if it passes your validation rules when it's being read from file, and after reading the end of file, you'd have array ready for writing it back to file.

OTHER TIPS

How about this for steps:

  • Read the file.
  • Store File contents in array.
  • Remove item from array.
  • Overwrite the file with new contents.

What you can do is opening the source file in read mode and a temporary file in write mode. As you read content from the "in" file, you write lines to the "out" file. When the "in" file is processed and closed, you rename "out" to "in". This way you need to worry less about memory constraints.

When processing each line, it's better if you split on '|', so you know that the second element contains an animal name and the third element contains a housing name. Who knows if a cat is living in a doghouse.

<?php
    $fileName = 'db.txt';

    $data = @file($fileName);

    $id = 0;
    $animal = "";
    $type = "";
    $number = 0;

    $excludeAnimal = array("dog");
    $excludeHouseType = array("house");

    foreach($data as $row) {
        list($id,$animal,$type,$number) = explode("|",$row);
        if(in_array($animal,$excludeAnimal) && in_array($type,$excludeHouseType))
            continue
        /* ... code ... */
    }
?>

Although this doesn't answer your original question, I'd like to share what I've come up with.

I'm pretty sure this will do your entire script in three lines:

$file = file_get_contents( 'db.txt');
$result = preg_replace('/^\d+\|dog\|house\|\w+$/m', '', $file);
file_put_contents( 'db.txt', $result);

It uses a regex to replace the lines with dog|house, then writes the file back.

  1. Read and dump all data until the one you want deleted into $array_1.
  2. Read and dump rest of file into $array_2.
  3. Concatenate 2 arrays in a $newarray, rewrite to original flatfile.

Simple!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top