1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'PHP & Perl' started by OTrap, Sep 14, 2012.

  1. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,286
    Likes Received:
    1,052
    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.
     
  2. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,286
    Likes Received:
    1,052
    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.
     
  3. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,286
    Likes Received:
    1,052
    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?
     
  4. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,153
    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.
     
    • Thanks Thanks x 2
  5. tripper_john_md

    tripper_john_md Newbie

    Joined:
    Feb 21, 2011
    Messages:
    40
    Likes Received:
    35
    Location:
    Southern Germany
    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.
     
    • Thanks Thanks x 1
  6. sidnettwo

    sidnettwo Junior Member

    Joined:
    Jun 5, 2009
    Messages:
    135
    Likes Received:
    60
    I think tripper_john_md has got it.

    If you donĀ“t want to write to files, then save the page load as an incremented number in a db, and load the array position from that saved number.
     
    • Thanks Thanks x 1
  7. CodingAndStuff

    CodingAndStuff Regular Member

    Joined:
    May 6, 2012
    Messages:
    236
    Likes Received:
    84
    Occupation:
    Swagstronaut
    Location:
    You can't have my bots. Sorry :'(
    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 Thanks x 1
  8. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,286
    Likes Received:
    1,052
    Thanks guys! It works! Thanks and Reps all around.