Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cleo

macrumors 65816
Original poster
Jan 21, 2002
1,186
0
Tampa Bay Area, FL, USA
Ok, so awhile ago I picked up a domain and wasn't sure what I'd ever use it for, but Barack Obama is Your New Bicycle totally inspired me. I want to create a similar effect: "random" word or phrase pulled from file/array, displayed as self-referential link, script runs again (thus new phrase) each time the link is clicked.

Here's the thing: I know enough PHP (and am a good enough researcher) to put something like this together, but I suspect it would not be efficient at all. I also suspect that for many other people, whipping up such a script would take no time flat. So... is anyone out there willing to help a girl out here? I'd definitely appreciate any pointers you can offer.

(BTW, my impulse, having only used "random" effects to a limited extent (like choosing which logo to show on my tumblelog, where there are only, like, 5 options) is to do something like

Code:
$x = rand(1,10);
if ($x == 1) { $phrase = "Macrumors is awesome";
elseif...

But, like I said, that doesn't seem very efficient, when there are 50+ quotes/phrases and I have no idea how much traffic will ensue.]
 
This may work for you. Just put all of the possible quotes into an array and then randomly access one of them.
PHP:
$quotes = array('Quote 1', 'Quote 2', 'etc');
$phrase = $quotes[rand(0, count($quotes)+1)];
echo $phrase;
 
You could also use a switch statement, which is going to be faster than a long series of if/else statements.
 
You could also use a switch statement, which is going to be faster than a long series of if/else statements.

Using switch in place of redundant if/else statements is good programming, but in this case, it wouldn't be very practical. Everytime time he decides to add a new phrase, he'd have to type:

case x:
"whatever phrase";
break;

Not only that, but he would also have to update his "$x = rand(1,10);" line after each case. That's 3 lines of code added and 1 line changed for every entry, which only increases the % of error as well as file size.

Angelwatts solution is much more simple and practical. For any new phrase he wants to add, he would just need to type a comma followed by the new phrase in the array. Nothing else needs to be changed.

improving on angelwatts solution, an even simpler way would be:
PHP:
$quotes = file("quotesfile.txt");
$phrase = $quotes[rand(0, count($quotes)+1)];
echo $phrase;

Doing it this way, all you would need to do is create a quotesfile.txt and just add every quote as a new line (no quotations needed). The file() function would read the quotesfile and return it as the same array($quotes) in angelwatt's example. Now there's no need to even type quotations and commas for the phrases.
 
Thx, all! I went with alfaphlex's variation, and it works great. The only problem is that every now and then, nothing appears (instead of a phrase). (The page footer appears, though, so it's not like the entire page goes fubar.) Could that be because of the

PHP:
count($quotes)+1

bit? I've been pondering the +1, and I just don't get it. Since rand is inclusive, shouldn't it be

PHP:
rand(1, count($quotes))

? :confused:
 
Thx, all! I went with alfaphlex's variation, and it works great. The only problem is that every now and then, nothing appears (instead of a phrase). (The page footer appears, though, so it's not like the entire page goes fubar.) Could that be because of the

PHP:
count($quotes)+1

bit? I've been pondering the +1, and I just don't get it. Since rand is inclusive, shouldn't it be

PHP:
rand(1, count($quotes))

? :confused:

When I tested without the +1 I was getting blanks like you are now, which is why I added it. I didn't have any empty occurrences with this though, but didn't test a whole lot. The documentation says it is inclusive so you'll need to start at 0 rather than one, and the +1 should possibly be -1 since the last index in the array is one less than than the number of items (because indexes start at 0). So try -1 I guess and see if that alleviates it.
 
Check that the quotes file doesn't include blank lines at the end. Often when we edit text files we hit Enter a few extra times and a bunch of blank lines pool at the bottom.

If you want to be really robust, add a while (strlen($phrase) < 1) condition around the code (do not include the line that loads $quotes from the file).
 
Good tip notjustjay. Like angelwatt stated, it should be -1 instead of +1 for the array count because arrays start indexing from 0 and not one. Didn't notice it earlier because my intention was only to create the $quotes array with the file() function.

So the rand part should be:
PHP:
rand(0, count($quotes)-1)

An easy way to make sure you get no empty results no matter how many empty lines are in your quotes.txt file:
PHP:
do{
   // Do this until you get a result that is not empty
   $phrase = $quotes[rand(0, count($quotes)-1)]; 
}while(rtrim($phrase) == NULL);

That way you can have as many empty lines as you want in your txt file, lol. So the whole thing should now be:
PHP:
$quotes = file("quotesfile.txt");

do{
   // Do this until you get a result that is not empty
   $phrase = $quotes[rand(0, count($quotes)-1)]; 
}while(rtrim($phrase) == NULL);

echo $phrase;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.