Tip of the day script



Part 1 – q.readme.txt
These three different scripts read a single line from a file containing quotes, or “one-liners” as they’re sometimes called. Of course, you need a file of quotes to begin with, so I included my own quotes file in the zip file.

The first one seeds the randoming function which selects a random quote from the quote array. The second one uses the shuffle function to select a random quote from the quote array.

The third one is a little more complex. On Windows machines, and on ‘nix machines where the permissions are set, the code creates the quotes counter file if it doesn’t already exist. Otherwise, you’ll have to create a blank file and set permissions before installing the script.

It selects the next quote each time the script executes. This is done by updating the element number of the array in a separate file.

In each example, the “quotes” file path and file name is the first line of code. The third example contains the “qfile” variable with the path and file name as the second line of code.

Part 2 – Quotes1.inc
< ?
$quote = file("quotes.txt");
srand((double)microtime()*1000000);
echo $quote[rand(0,count($quote))];
?>

Part 3 – Quotes2.inc
< ?
$quote = file("quotes.txt");
echo $quote[shuffle($quote)];
?>

Part 4 – Quotes3.inc
< ?
$quote = file ("quotes.txt");
$qfile = "qnumber.inc";
if (file_exists ($qfile)) {
$fp = fopen ($qfile,"r+");
$qc = fgets ($fp,4);
$qc = chop ($qc);
$qc += 1;
if ($qc == count($quote))
$qc = 0;
rewind ($fp);
fputs ($fp,substr($qc." ",0,4));
fclose ($fp);
echo $quote[$qc];
}
else {
$fp = fopen($qfile,"w");
$qc = 0;
fputs ($fp,substr($qc." ",0,4));
fclose ($fp);
echo $quote[$qc];
}
?>

Part 5 – Quotes.txt
Which should contain tip of the day and or quotes line by line.

Related posts

  • No related posts


  • Leave a Reply

    You must be logged in to post a comment.