Click to See Complete Forum and Search --> : Retrieve data from a .txt
I've tried several ways to to this but I could not resolve anything: I have done 2 html pages. First page contains a small image of a product. Under the image should be the price retrieved from the text file. If u click the image the secondary page opens, containing a larger image of the product. Under the image should be the same price retrieved from the same text file. Is there any script to manage this?
Nevermore
03-09-2003, 05:44 AM
Not with javascript. Does your server support PHP?
Yes, it does. That means that I should learn PHP instead searching easy ways like javascripts? :D If u could give me an easy way to use PHP to resolve my prob. I will be more than thanksfull. The only problem with PHP is that the website is already done, and it's a html website. I don't know how I could insert PHP atributes into an html page. Teach me master.
Nevermore
03-09-2003, 12:12 PM
If you have the html, you insert PHP code inside tags that look like this:
<?php
code
?>
Does you server support MySQL databases, as this would be a much easier way than getting the data from text files. Faster, too.
Yes, it does have support for MySQL. I know how a php code begins but I still nedd the code to resolve my problem 'cause it's some kind of emergency. After that I swear I'll start to learn PHP.:cool: :cool: :cool:
Nevermore
03-09-2003, 12:42 PM
How much information does it need to display - just the price or what else?
The PHP can generate the original page and the one with the image in if you want.
To open a file in PHP:
<?PHP
$filename = "yourfile.txt";
$fp = fopen($filename, 'rb');
$filevalue = fread($fp, filesize($filename));
fclose($fp);
echo $filevalue;
?>
If you are inserting PHP into a HTML file, you will most likely need to rename to .php...
Nevermore
03-09-2003, 12:46 PM
That will need a separate text file for each image, which is a bit of a waste to store only a few bits of data. A database would be faster, too.
Nevermore
03-09-2003, 12:50 PM
This code should do it ona database called images with the columns: id, source and price. The link to it would look something like this: display.php?id=insertid.
<?php
if ($id) {
$username="";
$password="";
$database="";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
//gets id
$query="SELECT * FROM images where id = $id";
$result=mysql_query($query);
$source=mysql_result($result,0,"source");
$price=mysql_result($result,0,"price");
PRINT ("<img src=\"$source\" alt=\"There should be a picture here.\" border=\"0\">");
PRINT ("The price for this is $price");
}
else {
PRINT ("Failed to find that image. Perhaps it didn\'t want to be found...");
}
?>
Nevermore
03-09-2003, 12:51 PM
You need to put your username, password and database name in too.
Thank you very much people! This really helps me. In case you want to see the website I was talking about please visit http://www.rtelgsm.ro . I'm sorry that the only version is in romanian but if u want to see the pages that I need PHP for please click the "Modele si Preturi" button.
P.S.: The website is still under construction!
Just as a note...
I posted how to read from a txt file because often times, it is a bit overwelming to try to set up a database unless you know what you are doing. You wouldn't necessarly need multiple txt files. It could be a pipe separated file, etc. But I definitely agree with you in that a database is the best way to do this if you are comfortable with it.
Nevermore
03-09-2003, 01:27 PM
Pyro - I was re-reading my comments and it wounded like I was being nasty - i'm sorry, I'm just tired.
Xuu - It looks like you could use the database to store much more info than just prices and pictures. If you need help setting up the database, just ask.
webby
03-09-2003, 03:53 PM
listen to cijori pyro sounds stupid and is totally obtuse
Nevermore
03-09-2003, 03:54 PM
Webby - STOP INTEFERING WITH EVERYTHING I POST!
webby - it sounds like you have a problem. Next time you post something like that, I will report it to the moderators.
What about this? http://javascript.internet.com/messages/footer-text.html
khalidali63
03-10-2003, 09:06 AM
The link you provided reads from a *.js filewhich is a javascript src file and a browser can read it.Reading from files other then .js and .css is not allowes in the current security model of JavaScript.One will have to resort to some third party add ons to get this done,If Pyro has given you a solution for this,that is prety impressive,I guess that will answer allots of peoples answer on this forum.
Cheers
Khalid
Originally posted by khalidali63
If Pyro has given you a solution for this,that is prety impressiveI've modified the code I posted just a bit. I should now be easier to use. The code snippet will open files from the server and print them to your page. You can open html, php, txt, etc... It will, of course, print these files out in HTML format, so if you want to print a .txt file the way you see it (ie. with line breaks), you would use something like this:
<?PHP
$filename = "yourfile.txt";
$fp = fopen($filename, 'rb');
$filevalue = fread($fp, filesize($filename));
fclose($fp);
$filevalue = str_replace ("\n", "<br>", $filevalue);
echo $filevalue;
?>