+ Reply to Thread
Results 1 to 9 of 9

Thread: displaying a different quote for each visitor

  1. #1
    krofunk's Avatar
    krofunk is offline x10 Sophmore krofunk is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    In your socks
    Posts
    211

    Red face displaying a different quote for each visitor

    First time I have ever even considered this...when I did it just made my head hurt anyway I am trying to make a page for a friend that displays a different quote (from a list preferably) each time someone visits the page.

    Its hard to explain so here is an example from an ugly looking site that managed to get it working http://www.stephenbrownadi.co.uk/ the sites developers won't share.

    I imagen that this can be achieved through Java Script - I am happy to work in java script I just know very little bout it

    I have already tried to google for the solution but evidently I am asking the wrong questions, thanks for any help in advance!

    If I helped you out then don't forget to give me rep

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: displaying a different quote for each visitor

    PHP would be a better choice as Javascript would only really be needed if you wanted to load the quote after the page has loaded, also PHP better will work for the few people who have Javascript disabled.
    Here is a quick script that should work:
    Code:
    <?php
    $quotesArray = Array();
    $quotesArray[] = 'Quote 1';
    $quotesArray[] = 'Quote 2';
    $quotesArray[] = 'Quote 3';
    $quotesArray[] = 'Quote 4';
    //etc
    $quoteIndex = rand(0, count($quotesArray)-1);
    echo $quotesArray[$quoteIndex];
    ?>

  3. #3
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    Re: displaying a different quote for each visitor

    Edit: You know what, forget that, lemon-tree's code is so much better. I'll be using it too now, if you don't mind ^^
    Last edited by Salvatos; 10-09-2010 at 11:52 AM.

  4. #4
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: displaying a different quote for each visitor

    Or even:

    Code:
    <?php
    $quotesArray = Array(
    	'Quote 1',
    	'Quote 2',
    	'Quote 3',
    	'Quote 4',
    );
    //etc
    $quoteIndex = rand(0, count($quotesArray)-1);
    echo $quotesArray[$quoteIndex];
    ?>
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  5. #5
    krofunk's Avatar
    krofunk is offline x10 Sophmore krofunk is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    In your socks
    Posts
    211

    Re: displaying a different quote for each visitor

    fantastic! worked flawlessly!

    If I helped you out then don't forget to give me rep

  6. #6
    bdistler's Avatar
    bdistler is offline x10 Lieutenant bdistler is an unknown quantity at this point
    Join Date
    May 2010
    Location
    Catalina AZ USA
    Posts
    349

    Re: displaying a different quote for each visitor

    I use a flat file then I can edit, add, delete quotes with-out change to code

    open file
    get randon line (quote)
    close file
    throw quote

    This would be a different (random) quote each time page is loaded
    to add control use session cookies

  7. #7
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: displaying a different quote for each visitor

    Quote Originally Posted by bdistler View Post
    I use a flat file then I can edit, add, delete quotes with-out change to code

    open file
    get randon line (quote)
    close file
    throw quote

    This would be a different (random) quote each time page is loaded
    to add control use session cookies
    that would require more work than to use Alex Mac's method. because you have to go open a file, generate number, separate the lines into numbers, and then return the quote. In his method you skip having to open another file and separating the line into numbers because the array is already enumerated.

  8. #8
    bdistler's Avatar
    bdistler is offline x10 Lieutenant bdistler is an unknown quantity at this point
    Join Date
    May 2010
    Location
    Catalina AZ USA
    Posts
    349

    Re: displaying a different quote for each visitor

    Quote Originally Posted by diabolo View Post
    that would require more work than to use Alex Mac's method. because you have to go open a file, generate number, separate the lines into numbers, and then return the quote. In his method you skip having to open another file and separating the line into numbers because the array is already enumerated.
    open file - one line of code
    get number of lines in file - one line of code
    gen random number <= then total lines - one line of code
    set pointer and get random line (quote) - one line of code
    close file - one line of code
    throw quote - one line of code

    six lines of code

    How many lines of code to store 100 quotes useing using Alex Mac's method ?

    How much work to edit a text file vs. an array within a source file ?

  9. #9
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: displaying a different quote for each visitor

    Code:
    $quotesArray = explode('<br />', nl2br(file_get_contents('quotes.txt')));
    $quoteIndex = rand(0, count($quotesArray)-1);
    echo $quotesArray[$quoteIndex];
    Shorter code in terms of lines, longer in terms of time taken, resources used.

    ~Callum
    Last edited by callumacrae; 10-16-2010 at 05:24 AM.
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

+ Reply to Thread

Similar Threads

  1. quote from the wonderful world of IRC
    By Russ in forum Off Topic
    Replies: 6
    Last Post: 01-26-2009, 11:47 AM
  2. Post your favorite quote
    By compwhizii in forum Off Topic
    Replies: 23
    Last Post: 12-12-2008, 05:40 PM
  3. Best Quote
    By Coldfirezz in forum Off Topic
    Replies: 93
    Last Post: 08-07-2008, 08:55 AM
  4. CSS Quote Problem
    By n4tec in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 05-11-2005, 07:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers