Results 1 to 5 of 5

Thread: saving to a list?

  1. #1
    Join Date
    Dec 2004
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default saving to a list?

    i want to set up a page, that will allow me to add somthing to a list and that list will be displayed on another page. i can probably figure out how to display the list, but how can i get it to save? i plan on using a form to save it, so can anybody help me out?

  2. #2
    Join Date
    Feb 2006
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    You could add to the list in a text file and have every new entry on a new line e.g.
    LIST ITEM 1
    LIST ITEM 2

    Then read the text file line by line to display the list.
    PHP Code:
    <?
    if($_POST['Submit']){
    $open fopen("textlist.txt","w+");
    $text $_POST['list']."\r\n";
    fwrite($open$text);
    fclose($open);
    echo 
    "File updated."
    }else{
    echo 
    "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo 
    "<textarea Name=\"list\"></textarea>";
    echo 
    "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
    </form>"
    ;
    }
    ?>
    View file line by line:
    PHP Code:
    $file file("textlist.txt");
    echo 
    "<ul>";
    foreach(
    $file as $text) {
    echo 
    "<li>".$text."</li>";
    }
    echo 
    "</ul>"

  3. #3
    Join Date
    Dec 2004
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thankyou so much.


    edit: i tried it out, and it overwrites it now instead of adding to it. is it supposed to do that?
    Last edited by darco9x2; 02-24-2006 at 03:57 PM.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If you want it to append, replace
    Code:
    $open = fopen("textlist.txt","w+");
    with
    Code:
    $open = fopen("textlist.txt","a+");
    Note that you'll have to seek to the beginning of the file before reading from it:
    Code:
    fseek($open, 0);
    and if you then want to write to it again, you'll need to seek back to the end:
    Code:
    fseek($open, 0, SEEK_END);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Dec 2004
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ok thanks that clears it up.

Bookmarks

Posting Permissions

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