www.webdeveloper.com
Results 1 to 10 of 10

Thread: Stepping through an array

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Posts
    10

    Stepping through an array

    Hey,

    I have pulled results from my database and stored them into an array. Now I want to step through the array when a next button is clicked and display the current value of the array.

    I have been messing around with code all day but have got nowhere. Is there any special way to do this?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    Is the array numerically and sequentially indexed? If so, just set the value of the button to be the next index (checking that it exists first, of course), then use that to access the array. If not, then it gets messier.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2003
    Location
    VA
    Posts
    419
    PHP Code:
    <?php
    $array 
    = array('one','two','three');

    $index = isset($_GET['index']) ? $_GET['index'] : 0;
    for(
    $i 0$i $index$i++)
        
    next($array);
    if(
    current($array) === false){
        
    reset($array);
        
    $i 0;
    }
    $currentIndex current($array);
    ?>

    <h1>Arrays!</h1>
    The current item is: <?=current($array)?> at key <?=key($array)?><br/>
    <a href="?index=<?=$i+1?>">Next</a>
    Cornflakes mmmm yummy

  4. #4
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    The current item is <?php echo $array[$i]; ?>

  5. #5
    Join Date
    Jan 2003
    Location
    VA
    Posts
    419
    Quote Originally Posted by Dasher View Post
    The current item is <?php echo $array[$i]; ?>
    I was demonstrating the use of array functions. And that's statement is always true. The keys aren't guaranteed to be numerical, which is why I used current.
    Cornflakes mmmm yummy

  6. #6
    Join Date
    Apr 2011
    Posts
    10
    Thanks for the help everyone. Kyleva your code worked perfectly thanks. Guess I just need to work on learning more about the for loops.
    I changed the code a little so that i could make a previous button as well and it works quite well, but i was just wondering if anyone could have a look at it and tell me if it was the right way to do it?

    PHP Code:
    if (isset($_GET['previndex'])){
                            $previndex = isset($_GET['previndex']) ? $_GET['previndex'] : 0;
                            for($i = current($namearray) + 1; $i > $previndex; $i--)
                                prev($namearray);
                                prev($storyarray);
                            if(current($namearray) === false){
                                reset($namearray);
                                reset($storyarray);
                                $i = 0;
                            }
                        $currentIndex = current($namearray);
                        }

    <h1>Arrays!</h1>
                The current item is: <?=current($namearray)?> at key <?=key($namearray)?><br/>
                 <a href="?previndex=<?=$i-1?>">Previous</a>   
                 <a href="?nextindex=<?=$i+1?>">Next</a>

  7. #7
    Join Date
    Jan 2003
    Location
    VA
    Posts
    419
    You over complicated that. Using my first code, make a new link to ?index=$i-1
    Cornflakes mmmm yummy

  8. #8
    Join Date
    Jan 2003
    Location
    VA
    Posts
    419
    I just got home. I replied via my phone earlier. Here is what the code would look like:
    PHP Code:
    <?php 
    $array 
    = array('one','two','three'); 

    $index = isset($_GET['index']) ? $_GET['index'] : 0;
    if(
    $index == -1)
        
    $index count($array) - 1;
    else if(
    $index == count($array))
        
    $index 0;

    for(
    $i 0$i $index$i++) 
        
    next($array); 
    $currentIndex current($array); 
    ?> 

    <h1>Arrays!</h1> 
    The current item is: <?=current($array)?> at key <?=key($array)?><br/> 
    <a href="?index=<?=$i-1?>">Previous</a> <a href="?index=<?=$i+1?>">Next</a>
    That should work. I didn't test it though.
    Cornflakes mmmm yummy

  9. #9
    Join Date
    Apr 2011
    Posts
    10
    That code works perfectly as well..I understand most of what I was doing wrong thanks so much for the help.
    There is just one thing that I am confused about, the FOR loop. I thought it was supposed to continue until the condition was met?
    This is just a test I wrote out to show the process the loop will go through:

    //set counter to 0, test if counter is less than index if yes increment the counter and jump to next array.
    //1st loop counter = 0, is 0 < 0 no, break loop
    //2nd time 1st loop counter = 0, is 0 < 1 yes, inc counter = 1, jump to next array
    //2nd time 2nd loop counter = 1, is 1 < 1 no, break loop
    //3rd time 1st loop counter = 0, is 0 < 2 yes, enter loop, jump to next array
    //3rd time 2nd loop counter = 1, is 1 < 2 yes, enter loop, jump to next array

    By the time index reaches 2 it will need to loop twice until the condition is met. Am I looking at this wrong?
    Does next($array) break the loop?
    I tried to find the answer but I was unable to and I am just confused on this..any help is always appreciated.
    Thanks

  10. #10
    Join Date
    Jan 2003
    Location
    VA
    Posts
    419
    For loops run while the condition is true. Once the condition is fake, the loop breaks. In my example, we initialize $i to 0. Our condition is to run while $i is less than the given query param $index (which is derived from $_GET which comes from the URL). If the condition is true, we set the internal pointer of the array forwards by using next(). Afterwards $i is increased by one ($i++). When $i is no longer less than $index, the loop breaks. Thus, we can call current() on the array which will return the item at the current internal pointer.

    This isn't horribly relevant, but the expression that gets run in the for loop after the conditiOon check happens exactly after the condition check, not after the code block. The reason $i increases after the code block is because I used a post increament ($i++) as opposed to ++$i.

    Hope this helps.
    Cornflakes mmmm yummy

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles