Page 1 of 1
Getting Next Item in Array
Posted: Mon Jul 23, 2012 7:20 am
by reyes99
Hi all, I am trying to get the next and previous value in an array to be able to reorganize a menu. I am reading the menu items in from the db. displayOrder is a numeric value that I have with values 100, 110, 120, 130 and I want to reorganize the menu by adding or subtracting from the next and previous number but I can't figure out how to get the next value. The next() function just displays the same value.
Code: Select all
while ($row = mysql_fetch_array($sql)) {
echo $row['displayOrder']."<br />";
next ($row);
echo $row['displayOrder']."<br />;
}
Thanks in advance
Re: Getting Next Item in Array
Posted: Mon Jul 23, 2012 8:49 am
by social_experiment
reyes99 wrote:I am reading the menu items in from the db. displayOrder is a numeric value that I have with values 100, 110, 120, 130 and I want to reorganize the menu by adding or subtracting from the next and previous number but I can't figure out how to get the next value.
Could you explain a bit more here; this bit is quite confusing

Re: Getting Next Item in Array
Posted: Mon Jul 23, 2012 9:47 am
by reyes99
Hi Sorry it's confusing. Let me try to explain it again.
I have some menu Items in a db and I am trying to reorganize the items using an up and down arrow. When I click on the down arrow I want to move the item down. I have a value called displayOrder where I number each item 100, 110 120... so I can reorder the items.
Home is 100
About us is 110
Contact us is 120
If I wanted to move About us bellow Contact us I want to be able to change the displayOrder to 121. To do this I need to know what the next value in the array is so I can add a 1 to the next value and save it with the new value.
That way I will display the menu and order it by displayOrder and it will look like this:
Home is 100
Contact us is 120
About us is 121
Thanks
Re: Getting Next Item in Array
Posted: Mon Jul 23, 2012 10:25 am
by pickle
This is a flawed philosophy. What happens if you want to move "Home" below "Contact Us"? Do you then have to find if there is any menu item with the value of 121, increment that, then renumber "Home"?
The easiest solution would be to simply renumber all the items every time you re-order. Or at the very least, just swap values so "Contact Us" is valued at 110, and "About Us" is 120.
Regarding your original question, you're misunderstanding how next() works. It advances the array pointer for an array. In this case, $row. You don't want to advance the array pointer in $row, you want $row to be the next row in the result set. To do that, just call mysql_fetch_array() again.
Also, the mysql extension is deprecated. You should be using the mysqli extension instead.
Re: Getting Next Item in Array
Posted: Mon Jul 23, 2012 2:54 pm
by reyes99
Thanks pickle, that makes more sense.