Displaying Content From A Text File
I’ve recently been working on a website for The Stoops. It’s a one page site with a fairly simple layout; The main thing was that they wanted to be able to have the option to update the gig listings themselves (otherwise they’re having to constantly go through the web developer.) Normally I’d jump into using databases along with PHP for dynamic content, but this felt a little bit like overkill. So I did some research for different ways of getting text to appear from a separate file onto the web page. Here is what I found:
I created a basic text file containing a list of gigs:
I then used the following code to display its contents within the HTML page:
Using the same text file from method1 but this time using the PHP function ‘file_get_contents,’ which returns the file as a string. The string can then be put directly to the screen like in method1 or you could try using string functions to format it:
I created the XML file:
On the final version I separated everything into neat files and used a callback function for ‘onReadyStateChange.’ This allows me to test if the file has been found and opened successfully. I also created a new function to contain the code for creating a XMLHttpRequest object.
Bare in mind that these examples will need to be on a server to work. I use WampServer myself for running and testing my web pages – http://www.wampserver.com/en/. If you have any web development solutions or tips feel free to share them in the comments. If you like upbeat synth-pop music then please check out http://www.thestoopsmusic.co.uk/; they have some pretty awesome songs.
(Sorry about the terrible formatting. First time sharing my code online and WordPress doesn’t seem to make formatting easy.)