Prestonc Posted January 4, 2018 Share Posted January 4, 2018 Hi allI'm not a web developer, but have a specific need, and am hoping PHP is the correct language to write this in. I have a text file with the following example data:TXT1-1-2-1TXT1-1-3-2TXT1-3-4-2TXT2-9-8-1TXT2-16-1-1The text file has hundreds of linesI would like to display the contents of the text file on a web page, maybe in groups of 8 or 16 at a time - not a priority, but nice.I'd like a check box next to each entry, or maybe a better solution if you have one.I would then like to export all checked entries to another text file after clicking a Go style button. This file should be over writable, so it can be used again. Is this possible using PHP, or should I look elsewhere? Hope that makes sense.I'm using Apache on Ubuntu 16.04Cheers Quote Link to comment Share on other sites More sharing options...
Barand Posted January 4, 2018 Share Posted January 4, 2018 It is certainly possible using PHP. Quote Link to comment Share on other sites More sharing options...
Prestonc Posted January 4, 2018 Author Share Posted January 4, 2018 Cool. Any idea how I'd go about the check box part? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 4, 2018 Share Posted January 4, 2018 It would depend on how typical your posted sample data is. If they are only short strings as posted I would consider making the checkbox values equal to the string. Otherwise, the checkboxes and line text inputs should be treated as arrays with their indexes being the line number from the input file. Note that only checked check boxes will be sent by the form. Quote Link to comment Share on other sites More sharing options...
Prestonc Posted January 4, 2018 Author Share Posted January 4, 2018 OK, changed my mind a bit - Anyone out there spot what I've done wrong? The output is just the word Array I have a dropdown populated by a text file of values. When I select 1 or more of these values, and submit, i was hoping to have the selected values sent to a text file. I just get Array First file: <form action="action.php" method="post" name="items"> <select id="host_id" name="host_id[]" multiple="multiple" size="20"> <option selected="selected">Please Choose One:</option> <?php // define file $file = '/home/user/hosts.list'; $handle = @fopen($file, 'r'); if ($handle) { while (!feof($handle)) { $line = fgets($handle, 4096); $item = explode('|', $line); echo '<option value="' . $item[0] . '">' . $item[0] . '</option>' . "\n"; } fclose($handle); } ?> </select> <input type="submit" name="submit" value="Submit" /> </form> __________________________________________ action.php <?php if(isset($_POST['host_id'])) { $data = $_POST['host_id'] . "\r\n"; $ret = file_put_contents('/home/user/hosts.list.web', $data); if($ret === false) { die('There was an error writing this file'); } else { echo DONE; } } else { die('no post data to process'); } ___________________________________________________________________ Thanks all Quote Link to comment Share on other sites More sharing options...
Barand Posted January 4, 2018 Share Posted January 4, 2018 $_POST['host_id] is an array of selected options so you need to process it is an array. open file foreach ($_POST[host_id'] as $id) { //write id to file } close the file An alternative for processing the array is to convert it to string then output the string $all_data = join("\r\n", $_POST['host_id']); file_put_contents('/home/user/hosts.list.web', $data); Quote Link to comment 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.