View Full Version : Dynamically Loads Text Files Into Page
JoeRice
01-05-2010, 12:39 AM
Hi guys!
I'm fairly new to this site and to scripting in general so I'l keep it simple. :p
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
bluewalrus
01-05-2010, 01:03 AM
There can be one do you have php or a server language?
djr33
01-05-2010, 01:10 AM
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).
JoeRice
01-05-2010, 01:33 AM
Wow thanks for the quick replies!
Yes, my server has PHP, and it seems like that would be the easiest way to go!
bluewalrus
01-05-2010, 02:12 AM
This would be your php page that displays the text files and has links to others.
<?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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.