Basically it opens a file called facts.txt, loads all the content into arrays separated by ::: and then randomly picks one of them and echos out the info.
The facts.txt file looks like:
In Arizona, typically, the heaviest rain falls during the summer thunderstorm season, or Monsoon, in our state. The rain can accumulate very quickly, resulting in flooded streets or washes, and can even cause deaths via flash flooding. In Phoenix, the greatest rainfall in a 24 hour period was 4.98 inches on July 1-2, 1911. This total is quite a bit less than the Arizona record of 11.4 inches, which fell on Workman Creek (near Globe) on September 4-5, 1970.:::
Although Phoenix residents may feel that their city MUST be the hottest place around at times, top honors go to Lake Havasu City, where the mercury climbed to 128
If you only want to display one message, and not a series of messages, why not just use a random number to point to one element of the array? Seems like a lot of code to get there :?
All i can say is i copied Kevin’s instructions to the T, files names the lot, the only
difference being the facts themselves and the box there enclosed in thats all, the
rest is identical…
Why not the last one? In Kevin’s example h has the last one set with ::: so thats what i have done
What problem with it cause, could it cause a black output, cause i do get that, rarely but i do?
Man that is an old hunk of code. I posted that in 2005. Works, though I changed it long ago so that the data file could be more consistent by every entry having the delimiter at the end (including the last line).
It is much simpler code:
<?php
############################################################################
# A Project of TNET Services, Inc.
############################################################################
#
# Project: TNET Weather Station Pages
# Module: random.php
# Purpose: Obtains a random entry from a text file containing records
# deliminted by :::
#
# Note that EVERY entry needs to end in :::
#
# Authors: Kevin W. Reed <[email protected]>
# TNET Services, Inc.
#
# Copyright: (c) 1992-2008 Copyright TNET Services, Inc.
############################################################################
# Configuration
############################################################################
$FileName = "filename.txt";
############################################################################
if ( file_exists($FileName) ) {
// Get content as an array
$contents = explode(":::",file_get_contents($FileName));
// Remove last blank entry
unset($contents[count($contents) - 1]);
// Randomize the content
shuffle($contents);
// Output First Entry
echo $contents[0];
}
?>
Basically what it does is if the file exists, it explodes into an array delimiting on the ::: delimiter the contents of the file, it then counts the number of records and removes the last one (would be blank), uses shuffle to randomize the data and outputs the first entry.
There is no need for a loop since it only takes the first entry.
The use of [color=red][b]<![CDATA[[/b][/color] ... [color=red][b]]]>[/b][/color] in the desc fields prevents the XML parser from trying to parse the contents of the description so that it can contain data that is not valid in an XML file. Otherwise it would fail on some browsers stating it contains invalid data.
Thanks for both scripts, I think i will switch to the xml version purely for ease of adding more facts
what i mean is, with the text format it can be a little harder more so when adding multiple facts
trying to find the end to add the ::: and making sure thats no spaces, I use word wrap on notepad
and it gets a bit confusing, this is in no way putting it down, this is just me, hence it will be a lot
easier for me to use the xml version