...

View Full Version : Changing page from JS to PHP



ArcticFox
10-06-2004, 11:06 PM
http://arctic.ithium.net/frame2.html

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)...
Thanks!


*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?

gsnedders
10-06-2004, 11:46 PM
<?PHP
$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.

ArcticFox
10-07-2004, 12:26 AM
http://arctic.ithium.net/frame2.php

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:

Nightfire
10-07-2004, 11:55 AM
Or from a txt file


<?php
$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

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

Nightfire
10-07-2004, 11:49 PM
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

ArcticFox
10-08-2004, 02: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?

gsnedders
10-08-2004, 06: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...

Nightfire
10-08-2004, 08: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.

ArcticFox
11-19-2004, 03: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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum