PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm trying to generate a sort routine using a sortid in a database with up and down buttons to move items up and down in the list. This is fairly common, but I'm a little confused on how to do this.
I've gotten the up button to work fine by grabbing the previous item array, but how can I grab the next item in the array to get the id?
Be careful with this tho, becuase on the next itteration of the loop, you will miss out one of the array values. So basically you would only read every other value, unless you reset the internal array pointer back to where is sould be
Pimptastic wrote:Be careful with this tho, becuase on the next itteration of the loop, you will miss out one of the array values. So basically you would only read every other value, unless you reset the internal array pointer back to where is sould be
PHP Manual Comment wrote:Take care when replacing code using reset()/next() with code using foreach as foreach does not update the array's internal pointer. This means you cannot, say, use next() to skip an element in foreach loop
It means you can use foreach() with next() for the purposes outlined by OP. Yet you should be careful and execute next() on each iteration. Otherwise your loop will get out of sync with itself. Consider the following snippet: