Re: Displaying Text From A Data File
| From: | Torsten Roehr | Date: | Sat, 04 Sep 2004 19:37:51 +0000 |
| Subject: | Re: Displaying Text From A Data File | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-36043@lists.php.net to get a copy of this message | ||
"Ron Piggott" <ron.php@actsministries.org> wrote in message
news:001b01c492a9$275ab1c0$95703ad8@ron...
> I am still new to PHP and I wanted to ask a question.
>
> I am wanting to create a PHP script to randomly display one of many
greeting
> messages on my web site. I was going to store them in a text based file.
I
> wanted PHP to randomly select one of them and display it each time the
> script was loaded.
>
> I am still new at PHP and I am not sure where to get started. But I was
> thinking about this and I am sure this has already been done many times
...
> I am not sure where to look on the web.
>
> Can any of you help me get started?
>
> Ron
Hi Ron,
put your messages in a text file line by line (each message on a separate
line). Then use the following code to echo out a random message:
$messages = file('/path/to/your/file.txt'); // file() returns an array
where each line is an array element
$randomKey = array_rand($messages); // will return a random array index
echo $messages[$randomKey];
Hope this helps.
Regards, torsten Roehr