Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a random line from a txt file and displaying on my web page. 3

Status
Not open for further replies.

rossmcl

Programmer
Apr 18, 2000
128
Hello

I have a text file with 100 lines of text.

I want to use a function in php to randomly select one of these lines of text every time my website is loaded, and display it on my website.

Is this possible with php? If so, how do I go about doing it?

(Sorry if this is a simple question, my knowledge of php is extremely limited)

Regards
rossmcl
 
Code:
<?php
$i = 1 ;
$ran = mt_rand(1,100) ;
$FH = fopen("test.txt","r") ;
while ( !feof($FH) ) {
    $content = fgets($FH,4096) ;
    if ( $ran == $i ) {
        echo $content ;
    }
    $i++ ;
}
?>

Note: code untested

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Code:
<?php

$message_array = file("message.txt");

$message = array_rand($message_array);

echo "$message_array[$message]";

?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks spookie, that worked a treat.

Next question (I am a real beginner!) - how do I embed that php into an html file.

(I cannot change my index.html to index.php, as it is already in use, and linked from a lot of other peoples sites.)

I need to call that .php file that has the above script you told me about, from my index.html?

Is this possible?

Thanks
Rossmcl
 
Drop this in index.html...

Code:
<script src="[URL unfurl="true"]http://www.yourwebsite.com/function.php"></script>[/URL]

...and drop the nifty random text code shown above into 'function.php'

- - picklefish - -
Why is everyone in this forum responding to me as picklefish?
 
Hi,

Unfortunately the above code won't work as PHP file executes at server side.

You can make use of SSI (server side include) to acheive what you want.
You may want to make sure if your web server supports it.

Then you can insert

Code:
<!--#include virtual="pathto/function.php" -->
in the place where you want the output of the PHP file.




--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Or you can make HTML file to be parsed by PHP, if keeping the file extension to .html is that important.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Unfortunately the above code won't work as PHP file executes at server side.
The above code will work because it is calling a server-side PHP script.

All of the PHP code in this thread is server side. The SCRIPT HTML snippet simply asks a static HTML page to open a separate server-side script (function.php) and display that dynamically created info in the client's browser. This is the same thing as calling an external PHP page as a frame or an iframe.

I offer this script tag to many static sites so that they can subscribe to the dynamic content issued from my own site. This gimmick is used everywhere on the internet, usually in pushing advertisements.

You simply place the SCRIPT tag into where you want the variable data to display. It should not go in the HEAD.

- - picklefish - -
Why is everyone in this forum responding to me as picklefish?
 
I have a related issue... what if the goal is to choose a random line from a very very large file... for example /usr/share/dict/words ???

It seems very gluttonous to load the entire file into memory, if all we need is one line. But is there any other way?
 
As a general rule, don't read an entire file into memory at once unless I know for a fact that it will stay small.

For something like picking a random word from the standard Linux word list, I will invoke "wc -l /usr/share/dict/words" externally. That will give me the number of lines in the file. For that specific file, I know how many words are in the file.

I then pick a random number between 1 and the number of words in the file and use PHP's fgets() function to read and discard every word up to the word I randomly selected.

Not very elegent, but it works.



My Linux box, though, has MySQL running on it. For the words list, I would create a table:

create table words
(
pkID int unsigned auto_increment primary key,
word varchar(50)
)

Then perform:

load data infile '/usr/share/dict/words'
into table words (word)

Now I can peform:

SELECT word FROM words ORDER BY rand() LIMIT 1

to get a random word from the word list.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top