Results 1 to 5 of 5

Thread: Dynamically Loads Text Files Into Page

  1. #1
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dynamically Loads Text Files Into Page

    Hi guys!

    I'm fairly new to this site and to scripting in general so I'l keep it simple.

    I'm looking for a script (or wondering if it's even possible!) to load different text files into a page's content based on the link that brought them there?

    eg. I want to have a html file called "post.html" that contains all the layout and formatting of my main page, and use it to display all the different posts (in the form of text files) that I upload. So when I'm on my home page and click on the link "latest post 1", it loads "post.html" which in turn loads the content from "latest post 1.txt".

    Is this possible, and if so, is there a script out there I can use?

    Thanks a lot for your help

    Joe

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    There can be one do you have php or a server language?

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You have to choose between two options:
    1. Server side code (like PHP). This means your server must have it installed (or you need access to install it). Basically, PHP generates html and then presents it-- it can use files, databases, and do lots of other operations with the code before it outputs it. This will also be seemless (the user can't tell), and it can be made secure (such as only allowing certain text files to be shown) if you'd like.
    2. Client side code (like Javascript). This requires nothing on the server, but it does require that every user has it available/enabled-- or they will not be able to view your content. A serverside backup to Javascript is always recommended. This code can also be messier when dealing with files because it is a fairly complex operation for Javascript (but very easy in PHP).
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Wow thanks for the quick replies!

    Yes, my server has PHP, and it seems like that would be the easiest way to go!

  5. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    This would be your php page that displays the text files and has links to others.
    PHP Code:
    <?php
    $page_on 
    $_GET['page'];
    $ext ".txt";
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <?php
    if (isset($page_on)) {?>
        <?php
        
    if (file_exists($page_on $ext)) {
            
    ?>
            <title><?php echo $page_on?></title>
            </head>
            <body>
            <?php
            $file_contents 
    file_get_contents($page_on $ext);
            echo 
    $file_contents;
            
    ?>
            </body>
            </html>
            <?php
            
    } else {
            
    ?>
            <title>Invalid Request</title>
            </head>
            <body>
            <h1>Invalid Page Request Please return to the previous page.</h1>
            </body>
            </html>
            <?php }?>
    <?php 
    } else { ?>
    <title>Normal Page Title</title>
    </head>
    <body>
    <h1>My Page</h1>
    <p>Page Content</p>
    <a href="?page=getthis">Sample link to Load File "getthis.txt"</a>
    </body>
    </html>
    <?php }?>
    In that same directory put your text files and link to them with <a href="?page=getthis">

    Where the name of the text file is after the "?page=" without the extenstion. The $_GET gets the the name after what the "page=" has. So if you want to change how the address bar appears change the link name to something like

    <a href="?youarehere=getthis">

    and $_GET['youarehere'];

    If you don't care about the extensions you can change all the ext's to ".txt" or remove them all together and write the links to these with the .txt in it. For example

    <a href="?youarehere=getthis.txt">

    Make sense?

    If any PHP coders see any things I should change with my coding please let me know as well. Thanks.
    Last edited by bluewalrus; 01-05-2010 at 01:26 AM.

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
  •