Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2006
    Posts
    3,970
    Tokens
    0

    Latest Awards:

    Default [PHP] Writing to text files [+REPS!!]

    I know how to write to a text file;

    PHP Code:
    <?php
    $FormData 
    $_POST["FormData"];
    if (!isset(
    $_POST['submit'])) { 
    ?>
    <form method="post" action="<?php echo $PHP_SELF;?>">
    Data:<br>
    <input type="text" style="width: 400px;" name="FormData"><br>
    <br><br>
    <input type="submit" value="submit" name="submit" size="50">
    </form>
    <?
    } else {
    $submit fopen("data.txt""a"); 
    fputs ($submit,implode,("\n")); 
    fwrite($submit,"$FormData");
    fclose($submit);
    echo 
    "Done!";
    }
    ?>
    But how do i change it so that the stuff writen from the form is at the top of the page rather than the bottom.

    +Rep for help

    Tom
    Lets set the stage on fire, and hollywood will be jealous.

  2. #2
    Join Date
    Jul 2005
    Posts
    1,653
    Tokens
    50

    Latest Awards:

    Default

    You have one if/else statement so it's easy... Look at it closely, current its saying if the form HASNT been posted, show the form. So, to get the PHP at the top, you wanna do if the form HAS been posted, do the PHP, and then if it has been posted then the only other else is HASNT so you show the form

  3. #3
    Join Date
    Jul 2007
    Location
    Swindon
    Posts
    990
    Tokens
    125

    Default

    Quote Originally Posted by RYANNNNN View Post
    You have one if/else statement so it's easy... Look at it closely, current its saying if the form HASNT been posted, show the form. So, to get the PHP at the top, you wanna do if the form HAS been posted, do the PHP, and then if it has been posted then the only other else is HASNT so you show the form
    which is what he wants, try chmod'in the text file to 777?

  4. #4
    Join Date
    Dec 2006
    Posts
    3,970
    Tokens
    0

    Latest Awards:

    Default

    Its not a problem with writing the data from the form... what happens is when someone submits some data it goes to the bottom of data.txt. How do i change it so it adds it to the top of data.txt?
    Lets set the stage on fire, and hollywood will be jealous.

  5. #5
    Join Date
    Aug 2006
    Location
    Manchester, UK
    Posts
    2,016
    Tokens
    141
    Habbo
    florx

    Latest Awards:

    Default

    Ok fopen the file then clear the fille and add to the file your data and the data from fopen. That will put it at the top.

  6. #6
    Join Date
    Jul 2005
    Posts
    1,653
    Tokens
    50

    Latest Awards:

    Default

    Quote Originally Posted by Eccentric View Post
    which is what he wants, try chmod'in the text file to 777?
    I misread, atleast I actually tried to help because I know what I'm talking about other than giving an answer which you obviously guessed and have no clue about.

    But yeah, you need to open the file first, store whats in it in a variable, and then write $yourtext . $originaltext to the file.

  7. #7
    Join Date
    Dec 2006
    Posts
    3,970
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by RYANNNNN View Post
    I misread, atleast I actually tried to help because I know what I'm talking about other than giving an answer which you obviously guessed and have no clue about.

    But yeah, you need to open the file first, store whats in it in a variable, and then write $yourtext . $originaltext to the file.
    Thanks

    +Rep
    Lets set the stage on fire, and hollywood will be jealous.

  8. #8
    Join Date
    Oct 2006
    Location
    Peterborough, UK
    Posts
    3,855
    Tokens
    216

    Latest Awards:

    Default

    Or you can just use the mode r+, simple as.


    visit my internet web site on the internet
    http://dong.engineer/
    it is just videos by bill wurtz videos you have been warned

  9. #9
    Join Date
    Aug 2004
    Location
    UK
    Posts
    11,283
    Tokens
    2,031

    Latest Awards:

    Default

    So basicly you want it to append the top of the file, rather than the bottom with the new data?

    Php doesnt (i dont think) have a function for this but its still quite easy to do.
    PHP Code:
    // Store the current file in to a varible.
    $fileinfo file_get_contents("data.txt");
    //set are info out so are new data is at the top.
    $newdata $FormData "\n" $fileinfo;
    //Overwrite are file with the new one
    file_put_contents  ("data.txt"$newdata);  
    echo 
    "Done!"

  10. #10
    Join Date
    Apr 2006
    Location
    Leamington Spa
    Posts
    1,375
    Tokens
    72

    Latest Awards:

    Default

    PHP Code:
    <?php
    $filename 
    "file.txt";
    ////////////////////////
    $current file_get_contents($filename);
    $new $_POST['FormData'] . "\n" $current;
    file_put_contents($filename,$new);
    ?>
    Not tested.

    http://us3.php.net/file_put_contents
    http://us3.php.net/file_get_contents
    Last edited by lolwut; 02-03-2008 at 12:34 PM.
    i've been here for over 8 years and i don't know why

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •