Getting Next Item in Array

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!

Moderator: General Moderators

Post Reply
reyes99
Forum Commoner
Posts: 38
Joined: Tue May 22, 2007 10:35 pm

Getting Next Item in Array

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Getting Next Item in Array

Post 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 :dubious:
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
reyes99
Forum Commoner
Posts: 38
Joined: Tue May 22, 2007 10:35 pm

Re: Getting Next Item in Array

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Getting Next Item in Array

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
reyes99
Forum Commoner
Posts: 38
Joined: Tue May 22, 2007 10:35 pm

Re: Getting Next Item in Array

Post by reyes99 »

Thanks pickle, that makes more sense.
Post Reply