displaying a different quote for each visitor

krofunk

New Member
Messages
216
Reaction score
5
Points
0
First time I have ever even considered this...when I did it just made my head hurt :rolleyes: 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/ :frown: 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 :rolleyes:

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

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
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];
?>
 

Salvatos

Member
Prime Account
Messages
562
Reaction score
1
Points
18
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:

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
Or even:

Code:
<?php
$quotesArray = Array(
	'Quote 1',
	'Quote 2',
	'Quote 3',
	'Quote 4',
);
//etc
$quoteIndex = rand(0, count($quotesArray)-1);
echo $quotesArray[$quoteIndex];
?>
 

bdistler

Well-Known Member
Prime Account
Messages
3,534
Reaction score
196
Points
63
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
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
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.
 

bdistler

Well-Known Member
Prime Account
Messages
3,534
Reaction score
196
Points
63
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 ?
 

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
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:
Top