Page 2 of 2

Re: IM A NOOB!

Posted: Tue Jun 24, 2008 11:37 pm
by RobertGonzalez
Have you ever programmed in PHP before?

Re: IM A NOOB!

Posted: Wed Jun 25, 2008 2:52 am
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.