Jump to content

Flat Database help


wansell

Recommended Posts

Hi All,

Sorry if this is in the wrong category, but it seems the flat database category in the forum has been closed down...

Anyway my problem. I would like to be able to edit/modify just one field in my flat database. The fields are seperated by "|". I have a form that you can edit all the fields, but i think this relys on the other form fields filling the other database fields.

I just want this script to open the databse change a number in the database in a particular field, save it and then close it.

Is this possible, and how would i acheive this.

Thanks
Wayne Ansell
Link to comment
Share on other sites

Not very sophisticated, but this will allow you to edit a flat (delimited text) file .

(Remove '_' from f_open, f_write, f_close)

[code]<?php
$filename = 'test.txt';
$delim = '|';

if (isset($_POST['submit'])) {
    $fp = f_open($filename, 'w');
    foreach($_POST['field'] as $record) {
        $data = join($delim, $record) . "\n";
        f_write($fp, $data);
    }
    f_close ($fp);
}

?>
<FORM action="" method="POST">
<TABLE border='1' cellspacing='0' cellpadding='4'>
    <?php
        $file = file($filename);
        $rec = 0;
        foreach ($file as $line) {
            $line = trim($line);
            $fields = explode($delim, $line);
            //headings
            if ($rec==0) {
                echo "<TR>\n";
                echo "<TD>Rec #</TD>\n";
                foreach ($fields as $k => $val) {
                    echo "<TD>\n";
                    echo 'Field ', $k+1;
                    echo "</TD>\n";
                }
                echo "</TR>\n";            
            }
            echo "<TR>\n";
            echo '<TD>',$rec+1,"</TD>\n";
            foreach ($fields as $val) {
                echo "<TD>\n";
                $sz = strlen($val);
                echo("<INPUT type='text' name='field[$rec][]'value='$val' size='$sz'");
                echo "</TD>\n";
            }
            echo "</TR>\n";
            $rec++;
        }
    ?>
</TABLE>
<INPUT type="submit" name="submit" value="Submit">
</FORM>[/code]
Link to comment
Share on other sites

When using Flatfiles, I like to use the serialize functions:

[a href=\"http://www.php.net/serialize\" target=\"_blank\"]http://www.php.net/serialize[/a]

Less headaches with explode (which can be very unreliable)
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.