Jump to content

Recommended Posts

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

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

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??

 

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);

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.