Terry Pennis
04-01-05, 11:22 AM
I'm looking for a way to create an HTML page that has some parts which are read from various parts of a text file.
Ideally what I want is to create an HTML page in the usual way and include special tags that when the browser parses the HTML it will look in the right part of the right file and get the text.
Imagine the text file contained:
123
456
789
I would want an html page that could display:
"Hello 123 what is your 456. I like 789."
Any ideas on the best (read easiest/quickest) way to do this?
Cheers,
Tezzer.
Aborted_Fetus
04-01-05, 03:02 PM
You can achieve this with Javascript.
I'm not familiar with file input, but once you figure out how to do that, outputing the values is fairly easy.
Here's an example in pseudocode:
var line = new Array(1000); // Creates a new array for file input.
var i = 0; // Counter
nextline=(READ LINE FROM FILE); // Read the first line from file
while(nextline != -1) // Set up loop to read through the file
{
line[i]=nextline; // Store next line in next value in the array
nextline=(READ LINE FROM FILE); // Read next line
i++; // Incriment counter
}
document.write("Hello "+line[0]+" what is your "+line[1]+". I like "+line[2]".") // Write values to document
That should work...hope I didn't forget anything, I haven't tested it. Something like that should do it, though. If you don't have much experience with Java or any other programming language, this probably won't help you, but, oh well.
Stryder
04-02-05, 07:25 AM
I'm not 100% sure if Javascript will be able to read a file (I never bothered coding file accessing to that degree) However I know that PHP, VBScript and Perl can do it.
I would suggest for compatibility reasons to use either PHP or Perl, however that will mean requiring the server which you are serving the files on to be capable of "Executing" the scripts.
In both Perl and PHP (and most other languages) it doesn't matter if it's a file being read or a database, you have to undergo a process of OPENING, READING and CLOSING.
(If you are writing a file, then you have to OPEN, WRITE, CLOSE with an option use of "LOCKING" the file so that when it's written to nothing else is attempting to read it at the same time otherwise you can end up with errors)
You might even find that FLASH and it's Activescripting will allow you to read a pages contents and format it into dynamic variables.
I suggest going to one of the many sites related to Perl, PHP, Flash or Java.
Yamayama
04-02-05, 09:16 AM
What you're looking for is a server-side language. I know little about server-side javascript, but - as Stryder said - you could certainly do what you want with either perl or PHP. Personally, I am only familiar with perl, and this is what I'll use in this example.
I would suggest that you use an XML file for storing your data. XML is easy to learn (similar to HTML), and is compatible with a huge number of applications.
Here's an example XML file - we'll save it as "people.xml" - that stores information on two people:
<?xml version="1.0" encoding="ISO-8859-1"?>
<people>
<person id="1">
<name>John</name>
<age>23</age>
<interests>Soccer</interests>
</person>
<person id="2">
<name>Sandra</name>
<age>29</age>
<interests>Classical music</interests>
</person>
</people>
As you can see, it's syntax is very similar to that of HTML, and it is very easy to learn. To read/write from this file, you could you use one of the many XML perl modules. Personally, I'm experimenting with XML::XPath, based on the XPath (http://www.w3schools.org/xpath/default.asp) language.
Here's some perl code which could be used to read from that file:
use XML::XPath; # These are the modules needed to read/write XML files
use XML::XPath::XMLParser; # You'll probably have to install them; they're not included in the standard perl distribution
print qq|Content-type: text/html \n\n|;
# Begin HTML output
print qq|<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Just a test</title>
</head>
<body>
|;
# Halt HTML output
# Now for the actual reading of the XML file
my $xp = XML::XPath->new(filename => "people.xml");
foreach my $node ($xp->findnodes('//person')->get_nodelist) {
$name = $node->find('name')->string_value;
$age = $node->find('age')->string_value;
$interests = $node->find('interests')->string_value;
print "<p>" . $name . " is " . $age . " years old, and is interested in " . $interests . "</p>\n"; # more HTML output
}
# More HTML output
print qq|
</body>
</html>
|;
# End HTML output
This would give the output:
John is 23 years old and is interested in Soccer.
Sandra is 29 years old and is interested in Classical Music.
Although the learning curve might be steep, and although reading perl for the first time will possibly go over your head (..just come back to it in a few months or whatever), I think that, once you have gained the 'knack', this is quite likely the most efficient (read easiest/quickest) method. I also think it's a good idea to use XML, seeing as the World Wide Web Consortium (W3C) is pushing it as a standard language, and as the number of applications with which it is compatible is vast.
( P.S. Although it says that the code is 'PHP Code', it isn't! I had to use the PHP Code tags because they are the only tags that allow me to include HTML-like tags in the code. )