IM A NOOB!
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: IM A NOOB!
Have you ever programmed in PHP before?
Re: IM A NOOB!
Well ... both your current posts seem to relate to the reading of text files.
The file() manual page is very enlightening.
Assuming that your text file exists in the same folder as the script and reads something like ...
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 ...
Maybe
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.
The file() manual page is very enlightening.
file — Reads entire file into an array
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 ...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.
Code: Select all
$lines = file('random_links.txt');
print_r($lines);Code: Select all
http://www.site1.com
http://www.site2.com
http://www.site2.comLets see ...
All good so far. But that example on that page looks tricky. Let's read a bit more.array_rand — Pick one or more random entries out of an array
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.
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.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.
Maybe
Code: Select all
echo "My Random Site Is :- " . $lines[array_rand($lines)];