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?
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
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.
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?
<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>
<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 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
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.
Bookmarks