I was wondering if anyone could help me generate previous/next buttons based on an array of all items.
This is my basic array:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => ITEM 1
)
[1] => stdClass Object
(
[id] => 5
[name] => ITEM 2
)
[2] => stdClass Object
(
[id] => 6
[name] => ITEM 3
)
[3] => stdClass Object
(
[id] => 7
[name] => ITEM 4
)
)
What I’m trying to do is:
Viewing: ITEM 1
Previous Button: ITEM 4
Next Button: ITEM 2
Viewing: ITEM 2
Previous Button: ITEM 1
Next Button: ITEM 3
Viewing: ITEM 3
Previous Button: ITEM 2
Next Button: ITEM 4
etc
I guess I’m really trying to turn my original array into another array of prev/next based on which item I am viewing.. if that makes sense..? Any help would be greatly appreciated!
There are many ways to do this, but you could use
array_shift/array_pushto cycle through the array of things. This doesn’t use the exact array you mentioned, but it should get you close to your solution.This prints:
Keep in mind that this does no membership test, so if $current isn’t in the array, you’ll end up in an infinite loop. Also, I’ll add the disclaimer that I’m sure there are probably better ways to do this, this is just one approach.