maria Posted September 10, 2010 Share Posted September 10, 2010 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/213043-deleting-a-line-from-an-array-with-a-delete-button/ Share on other sites More sharing options...
Zane Posted September 10, 2010 Share Posted September 10, 2010 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"; ?> < Quote Link to comment https://forums.phpfreaks.com/topic/213043-deleting-a-line-from-an-array-with-a-delete-button/#findComment-1109558 Share on other sites More sharing options...
maria Posted September 10, 2010 Author Share Posted September 10, 2010 Thank you so much and dont be sorry for nitpicking I need to learn!!! Quote Link to comment https://forums.phpfreaks.com/topic/213043-deleting-a-line-from-an-array-with-a-delete-button/#findComment-1109564 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.