View Full Version : Changing page from JS to PHP

10-07-2004, 12:06 AM

I think this should be simple, but Iíve no idea how to do this...

All I want is to take all the JS on the page and make PHP handle the random quotes onLoad. Something so the pagesource just shows the basics and the single quote...

Iím also guessing that Iíll have to change the page from .html to .php (which would be fine)...

*edit* now that Iím thinking about it, being able to pull the random quote from a list (seperated by a return carrage) in a text file might be easier to add quotes onto... is this possible, too?

10-07-2004, 12:46 AM
$quotes = array("Spirits flying through the air<br>Crossing over to the other side<br>To rest in peace and never die again<br><br>And to be with long gone friends",
"In the misty morning, on the edge of time<br>Weíve lost the rising sun, a final sign<br>As the misty morning rolls away to die<br><br>Reaching for the stars, we blind the sky",
"Where the hell have I been<br>Sleeping, lost, and numb<br>So glad that I have found you<br><br>I am wide awake and heading home"
$random = array_rand($quotes, 1);
echo $quotes[$random];

This does the first three quotes, it should be obvious enough how to add the rest.

10-07-2004, 01:26 AM

LOL! That's so cool! It takes a lot of the code out (making the page smaller) and is easy enough to add more quotes later! :D

I think Iím going to like this ďPHPĒ thing...

Thanks 404! :thumbsup:

10-07-2004, 12:55 PM
Or from a txt file

$randquotes_file = file("file.txt");
$randquotes = rand(0, sizeof($randquotes_file)-1);
$Random_Quote = $randquotes_file[$randquotes];
echo $Random_Quote;

Have each quote on a new line on the file

10-07-2004, 10:52 PM
Can you explain that Nightfire?

10-08-2004, 12:49 AM
I'll try to explain. It's not my strongest point, but here goes.

$randquotes_file = file("file.txt");

Creates an array called $randquotes, as file() reads a file into an array

$randquotes = rand(0, sizeof($randquotes_file)-1);

rand() chooses randomly between 2 int values
sizeof is basically the same as count(). This is counting all the lines, or values in the $randquotes_file array.

$Random_Quote = $randquotes_file[$randquotes];

Just calls the array with the value of $randquotes

It's not tested or anything, but I don't see why it shouldn't work

10-08-2004, 03:55 AM
Now, with the first PHP code, does the page on the visitorsí browser load faster than with all the JS?

Would it load slower if it has to grab the quote from a .txt file first, or even faster because of even less PHP code?

Iím assuming that the .txt file has to be CHMOD @ 0777...? Does this cause any security issues?

10-08-2004, 07:11 PM
The text file only has to be CHMOD 755, as it only has to read it, not write.

As for speed ain't got the time for testing...

10-08-2004, 09:38 PM
The speed difference shouldn't be that much different between the two. There'd only be thousandsths(sp?) of a second difference between them I'd imagine.

11-19-2004, 04:52 AM
Thanks, guys!

I was thinking about this script today and decided it's the best code on my entire site. :thumbsup:

I'm also thinking of putting one on my forums that takes the quotes from the .txt file, so people can submit their own - excellent!