You are here: Re: Recursively cycling through an menu array « PHP Programming Language « IT news, forums, messages
Re: Recursively cycling through an menu array

Posted by Jerry Stuckle on 12/07/07 19:37

Arne-Kolja Bachstein wrote:
> Hi Jerry,
>
> yes, I know that and I for myself would have implemented this
> structure in a simple id,parent_id table. But this is a given table
> model that I cannot change, there'd be too many parts of the
> application to change, so I'd really appreciate to do this using PHP.
>
> At the moment I am stuck with having an array like
>
> --- snip ---
> Array
> (
> [0] => Array
> (
> [position] => Array
> (
> [0] => 00
> )
>
> [title] => Title of menu item 00
> )
>
> [1] => Array
> (
> [position] => Array
> (
> [0] => 01
> )
>
> [title] => Title of menu item 01
> )
>
> [2] => Array
> (
> [position] => Array
> (
> [0] => 01
> [1] => 01
> )
>
> [title] => Title of menu item 01.01
> )
>
> [3] => Array
> (
> [position] => Array
> (
> [0] => 01
> [1] => 02
> )
>
> [title] => Title of menu item 01.02
> )
>
> [4] => Array
> (
> [position] => Array
> (
> [0] => 01
> [1] => 03
> )
>
> [title] => Title of menu item 01.03
> )
>
> [...]
> --- snap ---
>
> Do you think I could work with this and maybe give an example of how
> it could work?
>
> Thanks in advance
>
> Arne
>
>> The way to solve it is to normalize your database. Your db fails first
>> normal form.
>>
>> Try comp.databases.mysql for some help with redesigning your database.
>> Once that's done, the rest will be much easier.
>

I do understand your problem :-)

I see Toby gave you one answer. I personally don't like using eval(),
however. So I think I'd take a different approach. A little harder,
but still possible.

Warning - none of this is tested - but it might give you some ideas:

Assuming $fieldId has '1.1.1', etc., and $title has the page title, I'd
explode the array, then build an array element for it, i.e.


The function below recurses. First it checks to see if we've run out of
field id's in our array. If so, it sets the text in the item in element
'title'.

If we aren't past the end of the array, it checks to see if this item is
already defined as an array. If not, it makes an array.

Once the array is set, it recurses, passing the a subset of the $menu
array to add on to it.

So, assuming you have '1.2.3', 'My Page', The values passed would be:

$menu, Array(1,2,3), 0, 'My Page'
$menu[1], Array(1,2,3), 1, 'My Page'
$menu[1][2], 2, 'MyPage'
$menu[1][2][3], 3, 'MyPage'

And on the last one it would set $menu[1][2][3]['title'] to 'MyPage'

function setMenuItem(&$menu, &$idArray, $offset, &$title) {
if ($offset > count($idArray) { // If past end of the array
$menu['title'] = $title;
return;
$curIndex = $idArray[$offset]; // Just a working value
if (!isset($menu[$curIndex')) // If not set, make it an array
$menu[$offset] = array();
$setMenuItem($menu[$offset], $idArray, $offset+1, $title);
}

$menus = array();

// Loop here through each item returned:
$idArray = explode('.', $fieldId);
$setMenuItem($menus, idArray, 0, $title);


Now to display the menus, use another recursive function:

function showMenu (&$menu) {
if (!is_array($menu)) // Safety valve - shouldn't occur, but..
return;
foreach($menu as $key->$value) {
if ($key == 'title')
echo $value; // Display the menu item
else
showMenu($menu[$value); // Otherwise recurse
}
}

And to display it:

showMenu($menus);

As I said - completely untested, but it might give you ideas.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация