Jump to content


Photo

Flat Database help


  • Please log in to reply
2 replies to this topic

#1 wansell

wansell
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 01 June 2006 - 02:57 PM

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 01 June 2006 - 09:54 PM

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

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

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 02 June 2006 - 12:54 AM

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)
~ D Kuang




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users