Looking for how to render rotating text from a .txt list on a page

OTrap

Elite Member
Joined
Jul 12, 2008
Messages
2,314
Reaction score
1,064
I have a .txt file that is a list. It looks like this:
Code:
Jay, Cutler
Ben, Roethlisberger
Aaron, Rodgers
Drew, Brees
etc.



Now, I'm trying to have a PHP page that will pull a row of data from the .txt file and display the values separately, AND I need it to rotate down to the next line each time the page loads.

Can this be done?

If so, I'd appreciate it.

Reps and Thanks to whoever helps me figure it out.
 
I would think it would be a quick little fix. I'll spot someone a couple bucks via PayPal if they can help me do it.
 
Okay, I've figured out almost all of it.

The ONE thing I'm getting stuck on is the line below:

PHP:
$test[rand(0, (count($test) - 1))];

Obviously, this returns a random row from the .txt file. Given my inexperience, I don't know how to KEEP it from returning a random value. I'd like it to go in order. It was a copy/paste job by someone who knows almost nothing about PHP.

Can anyone tell me how to keep it from randomizing the output?
 
You must save the current line somewhere (e.g. in a file, in the db). Every time the script runs, it runs from scratch - there 's no persistence of memory between executions.
 
Read the file into an array, use the first line with $text[0] in every way you want, attach it to the end of the array and unset $text[0], then save the array to the file. This way the next line becomes the first line and you don't need to save any positions.
 
Since you want it to change depending on the page load, you'll either need to store the values in a database OR have it session/cookie based. This is possible if you add an identifier to each row for the script to know which value is which.

Example:

Code:
1,Smith,John
2,Saget,Bob
3,etc,etc

Then you could use the explode() function to split up the text buffer by newlines, then dump it back to a string and explode it again but this time use "," as the delimiter. From there you grab $array[0] to have the rows unique identifier.

This will allow you to be able to use PHP's set_cookie() function as a temporary storage unit for iterating through the rows per page load (much like the results of mysql_fetch_array(), except the second parameter being numeric, not associative).

Please note that this is a very unprofessional way to get it done and you should seriously consider using a database to store and manipulate this data (regardless of the fact that you'll have to use a little bit of logic to display it in the order described above).

Hope I helped!
 
Thanks guys! It works! Thanks and Reps all around.
 
Back
Top