holcomb Posted January 22, 2009 Share Posted January 22, 2009 So, I'm new to PHP and still learning, but can't seem to find the best way to achieve something. I have a flat txt file with entries in it, all on individual lines. I want to be able to read that file through PHP, generate a checkbox for each one, and print the contents of each line next to its own checkbox. I then want the user to be able to select an item (can be multiple at once) and choose 'delete' and it will wipe the contents of the original file and write new contents, which would be all of the UNselected checkboxes. I apologize if this sounds confusing, if further clarification is needed please let me know. Link to comment https://forums.phpfreaks.com/topic/141957-text-files-checkbox-generation/ Share on other sites More sharing options...
flyhoney Posted January 22, 2009 Share Posted January 22, 2009 A database would definitely be your best option But if you must use a txt file, something like this could work (untested): <?php // see if form was submitted if (isset($_POST['submit'])) { // open text file as an array $lines = file('data.txt'); // see which lines of the text file were 'checked' foreach ($_POST['lines'] as $line) { // removed checked lines unset($lines[$line]); } // array to string $text = implode("\n", $lines); // write updated line info to file $fp = fopen('data.txt', 'w'); fwrite($fp, $text); fclose($fp); } $lines = file('data.txt'); ?> <form method="post"> <?php foreach ($lines as $number => $text): ?> <input type="checkbox" name="lines[]" value="<?php echo $number ?>"> <?php echo $text ?> <?php endforeach ?> <input type="submit" name="submit" value="Submit" /> </form> *edit* fixed a typo Link to comment https://forums.phpfreaks.com/topic/141957-text-files-checkbox-generation/#findComment-743315 Share on other sites More sharing options...
kwstephenchan Posted January 22, 2009 Share Posted January 22, 2009 Hi! I have something similar except that I defined the name as box[] for array of checkbox, However, I got this error : Notice: undefined index: box in ... when trying to get the value from the array of checkbox $_POST['box[]'] Did you get the same message as I did when you try to evaluate the checkbox?? Link to comment https://forums.phpfreaks.com/topic/141957-text-files-checkbox-generation/#findComment-743445 Share on other sites More sharing options...
holcomb Posted January 22, 2009 Author Share Posted January 22, 2009 It worked, thanks flyhoney. Do you think you could point me in the direction of trying this with a database since that is what you recommend? Stephen, I did not encounter any issues other than needing to change implode("\n", $lines); to implode("r\n", $lines); Link to comment https://forums.phpfreaks.com/topic/141957-text-files-checkbox-generation/#findComment-743458 Share on other sites More sharing options...
kwstephenchan Posted January 22, 2009 Share Posted January 22, 2009 Thanks holcolm, I guess I need to work harder to solve the mystery especially when I am talking about the html part. Link to comment https://forums.phpfreaks.com/topic/141957-text-files-checkbox-generation/#findComment-743516 Share on other sites More sharing options...
flyhoney Posted January 22, 2009 Share Posted January 22, 2009 Change $_POST['box[]'] to $_POST['box'] Link to comment https://forums.phpfreaks.com/topic/141957-text-files-checkbox-generation/#findComment-743527 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.