Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Question Changing page from JS to PHP

    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?
    Last edited by ArcticFox; 10-07-2004 at 12:09 AM.
    <div> - putting your mind in a box since 1997

  • #2
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    PHP Code:
    <?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($quotes1);
    echo 
    $quotes[$random];
    ?>
    This does the first three quotes, it should be obvious enough how to add the rest.

  • #3
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Exclamation

    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!

    I think I’m going to like this “PHP” thing...

    Thanks 404!
    <div> - putting your mind in a box since 1997

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Or from a txt file
    PHP Code:
    <?php 
    $randquotes_file 
    file("file.txt"); 
    $randquotes rand(0sizeof($randquotes_file)-1); 
    $Random_Quote $randquotes_file[$randquotes]; 
    echo 
    $Random_Quote
    ?>
    Have each quote on a new line on the file

  • #5
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Can you explain that Nightfire?

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I'll try to explain. It's not my strongest point, but here goes.
    PHP Code:
    $randquotes_file file("file.txt"); 
    Creates an array called $randquotes, as file() reads a file into an array
    PHP Code:
    $randquotes rand(0sizeof($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.
    PHP Code:
    $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

  • #7
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    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?
    <div> - putting your mind in a box since 1997

  • #8
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    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...

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    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.

  • #10
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Thumbs up

    Thanks, guys!

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

    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!
    <div> - putting your mind in a box since 1997


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •