Jump to content

List text file contents, apply check boxes and export


Prestonc

Recommended Posts

Hi all

I'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-1
TXT1-1-3-2
TXT1-3-4-2
TXT2-9-8-1
TXT2-16-1-1

The text file has hundreds of lines

I 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.04


Cheers

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

$_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);
Link to comment
Share on other sites

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.