Jump to content


Photo

*SOLVED* Writing with php


  • Please log in to reply
6 replies to this topic

#1 Avendium

Avendium
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 June 2006 - 12:03 AM

How would go about taking a value from a form object and printing it on a line that I choose in an external file? for example. If i have the following in my external file, data.txt.

data1
data2
data3
data4

and i want the user to input a value and that value be written on line 2 so that when the submit button is pressed the txt will now show.

data1
(whatever the input was)
data3
data4

thanks

#2 poirot

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

Posted 13 June 2006 - 12:56 AM

To retrieve the contents from the file use the file handling functions, being file_get_contents() the easiest in this case.

Then, use str_replace() to replace data2 with the input. Something like:

$data = file_get_contents('data.txt');
$data = str_replace('data2', $_POST['data'], $data);

[a href=\"http://www.php.net/file_get_contents\" target=\"_blank\"]http://www.php.net/file_get_contents[/a]
[a href=\"http://www.php.net/str_replace\" target=\"_blank\"]http://www.php.net/str_replace[/a]
~ D Kuang

#3 Avendium

Avendium
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 June 2006 - 01:14 AM

hmm, this would work if i knew what "data2" was in order to replace it. The problem is, data 2 could be anything. Is there anyway to just target and replace the whole line without knowing what is there to begin with? and without replacing the first line?

what im trying to do is this:

I have my form in form.php. I want to send the data to my data.txt, on the line designated for that perticular form, in this case its line 2. So whenever the user inputs a new value, line 2 would be replaced with that new input. so i never know what line 2 holds to replace it.

thanks for your help.

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 June 2006 - 01:40 AM

Look at the file() function - [a href=\"http://ca.php.net/manual/en/function.file.php\" target=\"_blank\"]http://ca.php.net/manual/en/function.file.php[/a] - it reads a file into an array. Your 'data2' will be the second element of that array. You can then set the new value POSTed as equal to the second element and write the array back to the file. See the file write function for that.

Remember that the first element of an array is $arr[0] not $arr[1].

Create some code and we'll work with you from what you do.
Legend has it that reading the manual never killed anyone.
My site

#5 Avendium

Avendium
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 June 2006 - 03:25 AM

I managed to load the file as an array and display the line that i want but im having trouble inputing the value that i get from my form back into the array.

Form code
<form action="test.php" method="POST">
name: <input type="text" name="name" />
<input type="submit" value="Submit"/>
</form>

PHP code
<? 
    $filename = 'test.txt'; //filename
    
        if (file_exists($filename)) {
        $data = file($filename, 'r+'); //Initialize array

        //debug
        echo $data[0];
        
    } else {
           echo "The file $filename does not exist";
    break;
}
?>

my txt file has the lines

line0
line1
line2
line3

I can display them fine but im having trouble trying to figure out how to place the input back into the file to replace line1 which would be $data[1] in the array. What kind of write function should i use?

#6 Barand

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

Posted 13 June 2006 - 06:10 AM

try

if (isset($_POST['text'])) {

    $filename = 'test.txt'; //filename
    
    if (file_exists($filename)) {
        $data = file($filename); //Initialize array

            // replace second line
        $data[1] = $_POST['text'] . "\n";
        
            // rewrite the file
            // ## remove '_' in filesys functions
        $fp = f_open($filename, 'w');    
        foreach ($data as $line) {
            f_write($fp, $line);
        }
        f_close($fp);
        
    } else {
           echo "The file $filename does not exist";
    }
}

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

#7 Avendium

Avendium
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 June 2006 - 05:27 PM

Works great. Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users