Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Rotating in order, not random

    I'm using a small script that randomly rotates words/html through a single line from a text file. I'm trying to rotate the list in order so the next refresh would go down the list.


    <? $textfile ="qoutes.txt";
    $items = file("$textfile");
    $item = rand(0, sizeof($items)-1);
    echo $items[$item];
    ?>

  • #2
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is it possible to do this from using the code above to start with?

  • #3
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure -- instead of using rand() to select an element, just send your array through a foreach() loop.

  • #4
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for making it unclear =x Thanks for your input anyways. I don't want the characters to load all at the same page. I want a word to show up in order for every refresh.

    Here's another example:

    text file:
    hey
    how
    are
    you


    page.php
    Refresh Page load #1 Shows "hey"
    Refresh Page load #2 Shows "how"
    Refresh Page load #3 Shows "are"
    Refresh Page load #4 Shows "you"

  • #5
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try:
    PHP Code:

    <? 
    $textfile 
    ="qoutes.txt";
    $items file("$textfile");

    //If Session not set, set it
    if(!isset($_SESSION['vid']){
    $_SESSION['vid'] == 1;
    }else{
    //If session is set add one so the next item is displayed
    $_SESSION['vid']++;
    }

    //Subtract one so the item id's start at 0
    $itemid $_SESSION['vid']-1;

    //Output
    echo $items[$itemid];
    ?>
    Mike

  • #6
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm getting a parse error on line 6.

  • #7
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    Sorry, forgot a closing bracket.
    PHP Code:
    <?  
    $textfile 
    ="qoutes.txt"
    $items file("$textfile"); 

    //If Session not set, set it 
    if(!isset($_SESSION['vid'])){ 
    $_SESSION['vid'] == 1
    }else{ 
    //If session is set add one so the next item is displayed 
    $_SESSION['vid']++; 


    //Subtract one so the item id's start at 0 
    $itemid $_SESSION['vid']-1

    //Output 
    echo $items[$itemid]; 
    ?>
    Mike

  • #8
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I seem to get this error:

    Notice: Undefined variable: _SESSION in c:\program files\localhost\www\rotate\index.php on line 16

    Notice: Undefined variable: _SESSION in c:\program files\localhost\www\rotate\index.php on line 23

    Notice: Undefined offset: -1 in c:\program files\localhost\www\rotate\index.php on line 26

  • #9
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Try something like this
    PHP Code:
    <?php
    session_start
    ();
    $item file("qoutes.txt");

    if (
    $_SESSION['sess_row'] === NULL) {
    $_SESSION['sess_row'] = 0;
    } else {
    $_SESSION['sess_row']++;
    }

    echo 
    $item[$_SESSION['sess_row']];
    ?>

  • #10
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It rotates randomly but not in sequence. Does it have to have a session for it to rotate it in a sequence for every refresh?

  • #11
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nicklas
    Try something like this
    PHP Code:
    <?php
    session_start
    ();
    $item file("qoutes.txt");

    if (
    $_SESSION['sess_row'] === NULL) {
    $_SESSION['sess_row'] = 0;
    } else {
    $_SESSION['sess_row']++;
    }

    echo 
    $item[$_SESSION['sess_row']];
    ?>

    Ok, got it tested and its working sort of. After the text file runs out of qoutes, it gives me this error:


    Notice: Undefined offset: 4

    Example: I have 3 qoutes and on the fourth refresh it gives me an error with the error number going up one at every refresh.

  • #12
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can make your code safe from overflow by using a modulus operator.

    Code:
    <?php
    session_start();
    $item = file("qoutes.txt");
    $itemCount = count($item);
    
    if ($_SESSION['sess_row'] === NULL) {
    $_SESSION['sess_row'] = 0;
    } else {
    $_SESSION['sess_row'] = ($_SESSION['sess_row'] + 1) % $itemCount;
    }
    
    echo $item[$_SESSION['sess_row']];
    ?>
    What this does is instead of incrementing the index of the array by one, it divides the index+1 by the total itemCount and sets the index of the array to the remainder of that division.

    Or... you can just put an "if" statement in to check to see if you've gone past the end of your array.

  • #13
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator
    You can make your code safe from overflow by using a modulus operator.

    Code:
    <?php
    session_start();
    $item = file("qoutes.txt");
    $itemCount = count($item);
    
    if ($_SESSION['sess_row'] === NULL) {
    $_SESSION['sess_row'] = 0;
    } else {
    $_SESSION['sess_row'] = ($_SESSION['sess_row'] + 1) % $itemCount;
    }
    
    echo $item[$_SESSION['sess_row']];
    ?>
    What this does is instead of incrementing the index of the array by one, it divides the index+1 by the total itemCount and sets the index of the array to the remainder of that division.

    Or... you can just put an "if" statement in to check to see if you've gone past the end of your array.
    Thanks Fumigator, the script is working but when I try adding any character before the code like an html, it gives me

    <br />
    <b>Warning</b>: session_start(): Cannot send session cache limiter - headers already sent (output started at c:\program files\easyphp1-8\www\qoutes\index.php:1) in <b>c:\program files\easyphp1-8\www\qoutes\qoutes.php</b> on line <b>2</b><br />
    Here's what I put:
    Inspired Qoutes: <?php include 'qoutes.php' ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •