Jump to content

Archived

This topic is now archived and is closed to further replies.

wansell

Flat Database help

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

Share this post


Link to post
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]

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

×

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.