You're using what is called "flat-file" database.
Everything you do (add, edit, delete) will involve several lines of code.
Way more work than MySQL, but it is what it is.
Something like below ... but not exactly ... just to give you an idea.
PHP Code:
// you already have an array from previous read of the file.
// pretend your array is called, array1.
// let's say you want to delete "Red".
$delete="Red";
// open up your database (file) to write ... this is going to overwrite your file ...
$filename="data.txt";
$fh = fopen($filename, 'w') or die("can't open file");
$count_array=count($array1);
for ($i=0;$i<$count_array;$i++) {
$temp=explode("|",$array1[$i]);
$color=$temp[0];
$url=$temp[1];
if($delete){
if($delete == $color){
// do nothing
// this is the one to delete, so don't write this one to the file.
}
else{
fwrite($fh, "$color|$url|\n");
}
}// if delete
This will work if you're looking for a specific value (ie: Green):
PHP Code:
// Grab the file's rows in an array $rows = file( './data.txt' );
// look for `Green` (we trim all values because trailing line breaks, etc, are included) $key = array_search( 'Green', array_map( 'trim', $rows ) );
// Does the key exist? Remove it and rewrite the data if ( FALSE !== $key ) { unset( $rows[ $key ] ); file_put_contents( './data.txt', implode( '', $rows ) ); }
This will work if you're looking for a specific line number:
PHP Code:
// Grab the file's rows in an array $rows = file( './data.txt' );
// Does line 3 (offset 2) exist? Remove it and rewrite the data if ( isset( $rows[2] ) ) { unset( $rows[2] ); file_put_contents( './data.txt', implode( '', $rows ) ); }
This won't work though (for what you want): file_put_contents( './data.txt', implode( '', $rows ) );. That will write concurrently on a single line, not over multiple lines so it won't work in the next iteration of a run. Use this for the implode: implode(PHP_EOL, $rows) instead.
No matter what you do in a file handle, you cannot just yank a line. You need to yank it and overwrite the entire data in the existing file. This is why a database is the best option.
// look for `Green` (we trim all values because trailing line breaks, etc, are included) $key = array_search( $delete, array_map( 'trim', $rows ) );
// Does the key exist? Remove it and rewrite the data if ( FALSE !== $key ) { unset( $rows[ $key ] ); file_put_contents( 'data.txt', implode( '', $rows ) ); } header("location:?access"); }
Read the comments... file() will return each line as a whole, including new lines.
PHP Code:
// look for `Green` (we trim all values because trailing line breaks, etc, are included)
$key = array_search( 'Green', array_map( 'trim', $rows ) );
There is a flag you can pass to file() to avoid this, FILE_IGNORE_NEW_LINES, but I just left them intact for my examples.
Oh yes I see what you mean. Since you file without removing the linefeeds, then you still have the linefeeds while imploding.
I'm not sure I like that.
And nor should you expect this would work. Your criteria was scalar; it had no indication of a key | value relationship in the file so you cannot search using something as simple as there is here.
This now requires you to [s|f]scanf / list / explode the data and separate the "key" out so you can find where it is in the file. If they are always numerically incremented matching the line number, then you can still use $key - 1 as whatever the posted line number is instead of using an array_search. Kbluhm's post in #5 there shows how to do that with just a line number.
Is there a reason why you don't simply use a db for this? Issuing a delete command would be a lot easier.
I can only assume it's the learning-curve issue that comes with it.
Most likely there will be more things added to the database, and
there will be more "updates" to the data. Security, or lack of,
by using .txt files may also become an issue. We can advise all day
long, but OP needs to decide if learning MySQL will suit him/her best.
I went into my "php code vault" and came up with this code I used for spell checking. It uses the browsers spell checking features to check my text before I enter it into the database.
Create a spellcheck.txt and the html form reads the contents into the text field, you then edit the form and submit to re-write the txt file. I don't have php running to test it, but it was working the last time I used it.
spellcheck.php
PHP Code:
<?php if(isset($_POST['update'])) {
$update = $_POST['update']; $fh = fopen('spellcheck.txt', 'w') or die("can't open file"); fwrite($fh, $update); fclose($fh);