IM A NOOB!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: IM A NOOB!

Post by RobertGonzalez »

Have you ever programmed in PHP before?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: IM A NOOB!

Post by Stryks »

Well ... both your current posts seem to relate to the reading of text files.

The file() manual page is very enlightening.
file — Reads entire file into an array
Return Values

Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file() returns FALSE.
A quick look at your requirements suggests the information will be stored locally on your site, and that it will be a straight text file with a new URL on each line. So the first thing I'd try is ...

Code: Select all

$lines = file('random_links.txt');
print_r($lines);
Assuming that your text file exists in the same folder as the script and reads something like ...

Code: Select all

http://www.site1.com
http://www.site2.com
http://www.site2.com
Then you should get a result that outputs those sites. Now ... I'm sure there is a function to get a random array element. Lets search the manual for arrays. Hey look ... array_rand()

Lets see ...
array_rand — Pick one or more random entries out of an array
All good so far. But that example on that page looks tricky. Let's read a bit more.
Return Values

If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array.
Notes

Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
Hmmm ... seems easy enough. I dont need to call an array of values, just a single. And I shouldn't need call that srand() line with php5.

Maybe

Code: Select all

echo "My Random Site Is :- " . $lines[array_rand($lines)];
Okay ... now to go to that site. I hear header redirects are a good way to go. So perhaps a search for header() is next.
Post Reply