Forum Moderators: coopster

Message Too Old, No Replies

Customizing an Array...

Ordering a menu...

         

ahmedtheking

11:27 am on Jun 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello!

My menu is constructed via a mysql query that puts the data into an array.

Eg:
mysql_query >

array (
array ('title','link')
array ('title','link')
array ('title','link')
...
)

so on...

So each menu item will have an id, and i have sorted the arrays by their ids so that the menu is listed via the id.

is there a way to (via php, to be included in a cms) make a system to change the ids so that they menu structure could be changed?

mincklerstraat

3:36 pm on Jun 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you can. If your array called $array that looks something like this:

[0] => 'something',
[1] => 'somethingelse',
[2] => 'anotherthing';

the numbers are the 'keys' of the array, and the words are the 'values'. You can loop through your array using foreach(), which is sort of like a control structure tied to an array. It goes though the array element per element. So you can do:

foreach($array as $key => $value){
$array['number_'.$key] = $value;
unset($array[$key]);
}

doing
print_r($array)
would give you:
[number_1] => 'something', 
[number_2] => 'somethingelse'
... (etc).
You can't just put $key = 'number_'.$key inside that loop, since the key and value are just "copies" there, and not actual pointers to the real keys/values of the array.

coopster

11:53 am on Jun 24, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm guessing the id's in the database are unique, possibly auto_incremented id's ...

In cases like that, especially menu items or other items that we may want to sort by human decision rather than computer code/logic, I have found it beneficial to add a sequence number column to the table itself. Now we can control it's sequence any way we want.

ahmedtheking

10:17 am on Jun 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah that seems the most logical way! But then how can I make it so that...

Menu 1 > squence = 1
Menu 2 > squence = 2
Menu 3 > squence = 3
Menu 4 > squence = 4

...when menu 1 is moved down (therefore squence = 2) then menu 2's squence = 1? Automatically? Via JS or PHP?

coopster

1:25 pm on Jun 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You would certainly need to update it in the database accordingly.

But you are asking "how do I let the end user sort them"? JavaScript is a nice option, or you could display them on the maintenance screen with the menu sequence number and let them change that -- either of the two options, it's your choice and you need to know your intended audience of course ... will they have JavaScript running? If so, it certainly is easy for them to click an item and move it up or down until they get the sequence they want. Then all you have to do is loop through your menu and update sequence numbers.