Forum Moderators: coopster

Message Too Old, No Replies

flat file delete row

         

music_man

3:25 am on Jan 12, 2006 (gmt 0)

10+ Year Member



Hi

Does anyone know how to delete just a single row in a flat file database using php?

The "w" function deletes the whole file.

Is there a tutorial anyone knows about with examples of this?

dreamcatcher

8:29 am on Jan 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you need to do is read the contents of the file, then remove the line you need and then re-write the file back.

For example, say you want to remove line 5 and you are passing the info via a query string:

index.php?id=5

//read the contents of the file into an array

$file = file('myfile.txt');

//Loop through reading all lines except the one you want to delete

for ($i=0; $i<count($file); $i++)
{

if ($i!=$_GET['id']-1)
{
$data .= $file[$i]."\n";
}

}

Then delete the contents of your existing file and write the contents of $data back to the file, which will include the same data with the line you want removing missing. Something like that should do. The first slot in the array is 0, so to delete line 5, its 5-1 which is the 4th slot. Hope that makes sense.

dc

music_man

7:48 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



Hi

Thanks very much.

I have tried to implement it only it comes up with errors:

Warning: fopen() expects parameter 1 to be string, array

I am getting the id from a link.

Here is my code:

if($_GET["action"] == "delete") {

$file = file('events.txt');

//Loop through reading all lines except the one you want to delete

for ($i=0; $i<count($file); $i++)
{

if ($i!=$_GET['id']-1)
{
$data .= $file[$i]."\n";
}

}

$blank = "blank";

$fp = fopen($file,"w");
fwrite($fp,$blank); // Delete all the file
fclose($fp);

$fp2 = fopen($file,"a");
fwrite($fp2,$data); // Write information to the file
fclose($fp2);

}

dreamcatcher

10:52 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The $file variable is an array and can`t be used how you have it.

Try:

$fp = fopen('events.txt',"w");

dc

music_man

12:14 am on Jan 17, 2006 (gmt 0)

10+ Year Member



Hi

Thanks for your reply. Would that not just delete the whole file though?

dreamcatcher

9:37 am on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you loop you assign the new data to a variable with the exception of the line you want removing, then you clear or delete the original file and write the new data to it.

dc

music_man

11:00 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



exception of the line you want removing

I don't know how to find the line I would like to remove...

jatar_k

11:02 pm on Jan 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



does it have any recognizable characteristics?

music_man

11:36 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



Well the script is like this:

entrydelete.php?action=remove&id=4g8o0df

Where the id is unique.

In mysql I would use a WHERE I think.

dreamcatcher

11:41 pm on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use the strstr function to detect if that id exists when you loop. The function will return true if the data is found and false if not.

[uk.php.net...]

$file = file('myfile.txt');
$data = '';

for ($i=0; $i<count($file); $i++)
{

if (!strstr($file[$i],$_GET['id'])
{
$data .= $file[$i]."\n";
}

}

//Then delete the original file

if (file_exists('myfile.txt'))
{
unlink('myfile.txt');
}

//and write the new data

$fp = fopen('myfile.txt',"w");

if ($fp)
{
fwrite($fp,$data);
fclose($fp);
}

dc

music_man

7:52 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



Thanks

It sort of worked. It deleted the name I think, but nothing else. I am not sure it formatted correctly when writing it back.

Here is my posting events workings page:

<?
#####################
#
# Events compose
#
#####################
session_start();
require_once("includes/config.php");
if($_SESSION["valid"] == true)
{
if($_POST["action"] == "send")
{
if ($_POST["imagefile"]!= "") {
if (($_FILES['imagefile']['type'] == "image/jpeg")¦¦($_FILES['imagefile']['type'] == "image/gif"))
{
copy ($_FILES['imagefile']['tmp_name'], "gallery/".$_FILES['imagefile']['name'])
or die ("Could not copy");
}
else
{
echo "
<br>
<br>
";
echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")
<br>
";
}
}
$title = $_POST["title"];
$timearrive = $_POST["timearrive"];
$date_start = $_POST["date_start"];
$date_finish = $_POST["date_finish"];
$about = $_POST["about"];
$status = $_POST["status"];
$imageornot = $_POST["chkbox"];
$image = $_FILES['imagefile']['name'];
$id = rand(1000, 9999);
$filename = 'events.txt';
$pipe = "¦";
$cr = "\n";
$data1 .= $title . $pipe . $timearrive . $pipe . $date_start . $pipe . $date_finish . $pipe . $about . $pipe . $id . $pipe . $status;
if ($imageornot =="y") {
$data2 .= $pipe . $imageornot . $pipe . $image;
}
$data3 .= $cr;
$dataall = $data1 . $data2 . $data3;
$fp = fopen($filename,"a");
if($fp){
fwrite($fp,$dataall); // Write information to the file
fclose($fp); // Close the file

Here is the events delete page with your code implemented:

if($_GET["action"] == "delete") {

$file = file('data/events.txt');
$data = '';
for ($i=0; $i<count($file); $i++)
{
if (!strstr($file[$i],$_GET['id']))
{
$data .= $file[$i]."\n";
}
}
//Then delete the original file
if (file_exists('events.txt'))
{
unlink('events.txt');
}
//and write the new data
$fp = fopen('events.txt',"w");
if ($fp)
{
fwrite($fp,$data);
fclose($fp);
}

}

dreamcatcher

9:28 am on Jan 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are opening the file 'data/events.txt', then writing to 'events'txt'. Shouldn`t you have the directory?


f (file_exists('data/events.txt'))
{
unlink('data/events.txt');
}
//and write the new data
$fp = fopen('data/events.txt',"w");
if ($fp)
{
fwrite($fp,$data);
fclose($fp);
}

dc

music_man

4:41 am on Jan 22, 2006 (gmt 0)

10+ Year Member



Hi

It gives a permission denied warning for unlink but it still deletes the information...

Another weird thing is that when there are 2 entries and I click delete of one, it deletes it fine (with the warning) only when I visit the events page I can see the "Delete" link of the one I deleted with no name before it.

The events.txt file shows that there is no entry there but there is a space. When I delete the space, it doesn't show the "delete" link for the entry - it has gone.

Any ideas?

music_man

4:37 am on Jan 23, 2006 (gmt 0)

10+ Year Member



... bump....

jatar_k

5:05 pm on Jan 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> It gives a permission denied warning for unlink but it still deletes the information

check the permissions/owner on the file, then compare that with the user that php runs as

I would fix your warning first then look at the rest as there may be some connection. I don't quite understand the other problem but it almost sounds as if the data is gone but you are leaving a carriage return/newline so that you get a blank showing up.

music_man

7:56 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



Thanks for your reply. I am pretty sure that I have full privilages of the files.

Just wondering about the spaces...

music_man

7:35 am on Jan 25, 2006 (gmt 0)

10+ Year Member



... bump...

dreamcatcher

8:28 am on Jan 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe whitespace?

Try:

$dataall = trim($data1 . $data2 . $data3);

dc

music_man

4:57 am on Jan 26, 2006 (gmt 0)

10+ Year Member



I got it going!

It was writing a line space in the events_delete lol.

Deleted that and it works fine. Now to sort out about editing the entry,..