[Tutorial] - Random Quote

Example of PHP code.

Moderator: Moderators

[Tutorial] - Random Quote

Postby WinSrev on Fri May 06, 2005 6:21 pm

Lots of people search the internet for weeks trying to find an advance version of this, but why not use a version like this? its powerfull.

Code: Select all
<?
   $quoteFile = "quotes.txt";  //File with quotes, a new quote should a new line in this document
   $fp = fopen($quoteFile, "r"); 
   $content = fread($fp, filesize($quoteFile));
   $quotes = explode("\n",$content);   
   fclose($fp);   
   srand((double)microtime()*1000000); 
   $index = (rand(1, sizeof($quotes)) - 1);
   echo $quotes[$index];
?>


Any comments or questions please post. Thanks, :D
User avatar
WinSrev
Registered User
Registered User
 
Posts: 691
Joined: Sat Nov 27, 2004 10:44 pm
Location: UK

Postby robbie on Sun Jun 05, 2005 8:38 am

you could shorten your code by using file($quoteFile); instead of opening, reading then exploding :)
User avatar
robbie
Coder
Coder
 
Posts: 67
Joined: Fri Mar 12, 2004 4:40 am
Location: New Zealand

Postby Phantom on Sun Jun 05, 2005 1:39 pm

You could also do something like this:

Code: Select all
<?php
$title[0] = "Quote 0";
$title[1] = "Quote 1";
$title[2] = "Quote 2";
$title[3] = "Quote 3";
$title[4] = "Quote 4";
// get a random number
$randomize = rand(0, 4); // Change four to how many arrays you have
echo ($title[$randomize]);
?>

This can also be used for random titles.
"Best of the best of the best, sir! With honors!" -- MIB
[ jphantom.com ]
Phantom
Registered User
Registered User
 
Posts: 245
Joined: Mon Apr 18, 2005 3:24 am


Return to PHP Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest