Jump to content

Deleting a line from an array with a delete button


Recommended Posts

Hello,

 

I am new to php, and what I need to know and learn is a part of an assignment I normally wouldn't ask for help on the net for an assignment but my teacher is not very helpful and other students are struggling too! I have searched the net inside and out and have tried many things. My problem is that I am trying to remove a line from an array by clicking a delete button.

 

The outpur I need is:

 

a line of text that comes from my text file after it is exploded into an array | With a delete button here

next line of text that comes from my text file after it is exploded into an array | With a delete button here

etc

 

I have managed to write this much myself -

<?php
$file = fopen('fav/fav.txt', 'r');
$file = file_get_contents('fav/fav.txt');
$file_array = explode("\n",$file);
array_pop($file_array);
foreach($file_array as $line) {
echo "<form method='post' action=''>".$line.
"<input type='submit' name='post' value='delete'><br>";
$fh = fopen("fav/fav.txt",'w');
foreach ($file_array as $line) {
fwrite($fh,$line."\n");
}
fclose($fh);
} 
?>

The array_pop deletes from the bottom instead of the line the button is next too, I realise I will need to use and if statement but this is the closest I have gotten.

Thanks in advance!!!

 

Just to nitpick your code a little.  Your are assigning two different things to $file, both are different.  Meaning, the file_get_contents will overwrite the file handler returned by fopen.  So the first line in your code is useless.

 

Secondly, if you want a quick array of EVERY line in a file, use the file() function, it does exactly this.

 

Thirdly, in your loop, you are creating a NEW form for every single line, which isn't that bad, but it isn't needed either.  It's best practice to only have one form, unless the other form has a different purpose (like a login)

 

The code below should help you out.

$file = file("yourfile.txt");
if(isset($_POST['post']) && !empty($_POST['post']) {
    unset($file[$_POST['post']]);
     $fh = fopen("yourfile.txt",'w');
     foreach ($file as $line)
          fwrite($fh,$line."\n");
     fclose($fh);
}

echo "</pre>
<form method="'post'" action="''">\n";
foreach($file as $line)
     echo $line . "
\n";
?>
<

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.