Johng123 Posted January 12, 2010 Share Posted January 12, 2010 I am trying to now make my site edit like different text files and delete certain lines. What I wanted to do was be able to do was list out the text line by line with like a checkbox next to them and the option to delete that line. I don't know how to do this though because the size of the text file is always changing and I need to have the checkboxes come up next to each line with option to delete. I am pretty sure I cannot do this because I would probably have to make a form right? I don't know how with php this can be done. PLEASEEE HELP! Quote Link to comment https://forums.phpfreaks.com/topic/188137-dont-know-how-i-should-go-about-doing-this/ Share on other sites More sharing options...
oni-kun Posted January 12, 2010 Share Posted January 12, 2010 You can do it your method, but it is not such a good idea as it will require much more than a form, but a database. fgets / file_get_contents can pull out the file, to get each line you can simply explode it. $file = file_get_contents('file.txt'); $lines = explode('\n', $file); unset($lines[22]); //22'nd line. foreach ($lines as $line) {{ echo $line; //Display all lines. } Each will fill it's own array item, so you can do what you want with it, array search text you don't wish etc. Quote Link to comment https://forums.phpfreaks.com/topic/188137-dont-know-how-i-should-go-about-doing-this/#findComment-993277 Share on other sites More sharing options...
Johng123 Posted January 12, 2010 Author Share Posted January 12, 2010 You can do it your method, but it is not such a good idea as it will require much more than a form, but a database. fgets / file_get_contents can pull out the file, to get each line you can simply explode it. $file = file_get_contents('file.txt'); $lines = explode('\n', $file); unset($lines[22]); //22'nd line. foreach ($lines as $line) {{ echo $line; //Display all lines. } Each will fill it's own array item, so you can do what you want with it, array search text you don't wish etc. I liked the splitting of them into arrays, but I still don't know how I would let the user like delete specific ones, or make a form that contains $line amount of checkboxes Quote Link to comment https://forums.phpfreaks.com/topic/188137-dont-know-how-i-should-go-about-doing-this/#findComment-993279 Share on other sites More sharing options...
oni-kun Posted January 12, 2010 Share Posted January 12, 2010 $file = 'I \n am \n a \n line \n number did you \n know \n this? \n \n'; $lines = explode('\n', $file); for ($i=0;$i<=(count($lines)-1); $i++) { echo ($i+1) . "." . $lines[$i] . "\n<br/>"; } $delete = explode(',' , '1,2,3,4,5'); for ($i=0;$i<count($delete);$i++) { $lines[$i] = ""; } Rough method, but it will list something like: 1. I 2. am 3. a 4. line 5. number did you 6. know 7. this? 8. 9. The user can enter the lines they want deleted into the text box, $deleted can be the $_POST value (for ease) of it. Anc after the said lines are deleted (with the example code): So if $deleted was '1,2,3,4,5' : 1. 2. 3. 4. 5. 6. know 7. this? 8. 9. Then you can use implode and file_put_contents to put the file back. No \n means that the line will disappear, and not show in the result file. Quote Link to comment https://forums.phpfreaks.com/topic/188137-dont-know-how-i-should-go-about-doing-this/#findComment-993283 Share on other sites More sharing options...
Johng123 Posted January 12, 2010 Author Share Posted January 12, 2010 $file = 'I \n am \n a \n line \n number did you \n know \n this? \n \n'; $lines = explode('\n', $file); for ($i=0;$i<=(count($lines)-1); $i++) { echo ($i+1) . "." . $lines[$i] . "\n<br/>"; } $delete = explode(',' , '1,2,3,4,5'); for ($i=0;$i<count($delete);$i++) { $lines[$i] = ""; } Rough method, but it will list something like: 1. I 2. am 3. a 4. line 5. number did you 6. know 7. this? 8. 9. The user can enter the lines they want deleted into the text box, $deleted can be the $_POST value (for ease) of it. Anc after the said lines are deleted (with the example code): So if $deleted was '1,2,3,4,5' : 1. 2. 3. 4. 5. 6. know 7. this? 8. 9. Then you can use implode and file_put_contents to put the file back. No \n means that the line will disappear, and not show in the result file. thats a big help thanks man, just a few questions. How would I delete the entire line before it too like so if they typed in 5 its not just a blank line, but is instead removed. Also would the form look like this? <form action="newlist.php" method="post"> Lines: <input type="text" name="deleted" > <input type="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/188137-dont-know-how-i-should-go-about-doing-this/#findComment-993287 Share on other sites More sharing options...
oni-kun Posted January 12, 2010 Share Posted January 12, 2010 Yeah it would look like that, and be called through $_POST['deleted'] With the physical '\n' removed from the line, it will not show up as a blank line on the outputted page, as without the return it will just 'collapse'. Quote Link to comment https://forums.phpfreaks.com/topic/188137-dont-know-how-i-should-go-about-doing-this/#findComment-993290 Share on other sites More sharing options...
Johng123 Posted January 12, 2010 Author Share Posted January 12, 2010 Yeah it would look like that, and be called through $_POST['deleted'] With the physical '\n' removed from the line, it will not show up as a blank line on the outputted page, as without the return it will just 'collapse'. I don't know what you mean exactly. I would text that code right now but I don't really feel like configuring it for my site right now I'll do it in the morning, but does that mean that there will or will not be any blank lines with that code? Quote Link to comment https://forums.phpfreaks.com/topic/188137-dont-know-how-i-should-go-about-doing-this/#findComment-993305 Share on other sites More sharing options...
oni-kun Posted January 12, 2010 Share Posted January 12, 2010 Yeah it would look like that, and be called through $_POST['deleted'] With the physical '\n' removed from the line, it will not show up as a blank line on the outputted page, as without the return it will just 'collapse'. I don't know what you mean exactly. I would text that code right now but I don't really feel like configuring it for my site right now I'll do it in the morning, but does that mean that there will or will not be any blank lines with that code? You're not aware of whitespace? Take this line i'm writing now for instance The actual line in code is 'Take this line i'm writing now\n for instance'. That is why exploding \n will return all the lines. \n is the equivalent of pressing 'enter'. OS's just takes it and displays a return in place. So yes, if you remove the \n return, it will not display a line (so the blank line would disappear). Quote Link to comment https://forums.phpfreaks.com/topic/188137-dont-know-how-i-should-go-about-doing-this/#findComment-993321 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.