Please take your time to help me with this problem.
I'm trying to create a menu hierarchy structure.
And it kind of works.... the problem is that it takes 14 seconds to build the structure out of approx 150 menu items!:eek:
Now, the way I though would be the best way, was to read every menu item into an 2D array. That way, users accessing the webpage will only need to read the menu items only once from the DB server (it will be stored in a session variable).
At the same time, I read the same data into a 3D array. The 3D array is my main structure.
result = @mysql_query('SELECT _ID, _Parent, MenuName FROM ws_menustructure') or die (mysql_error());
// Put fetched data into a managable array
while ($row = mysql_fetch_assoc($result))
{
$arrResult[$i]['Parent']= $row['_Parent'];
$arrResult[$i]['MenuName']= $row['MenuName'];
if ($row['_Parent'] == 'Components')
{
$arrMenu[$counter]['Parent'][0][0] = $row['_Parent'];
$arrMenu[$counter]['MenuName'][0][0] = $row['MenuName'];
$counter++;
}
$i++;
}
Alright, my 2D array contais all menu items (around 150) and the 3D array contains Level 1 menu items.
The 3D array looks like this.
$arrMenu = array(
array( 'Parent' => array(),
'MenuName' => array()
),
array( 'Parent' => array(),
'MenuName' => array()
),
array( 'Parent' => array(),
'MenuName' => array()
)
);
Since I don't want to mess with my main structure, I make a copy of it -> $arr3DDummy.
Up til now evereything is allright. This is the part where I think my code is ineffective.
//Build 3D Menu Structure
for ($a=0; $a < sizeof($arr3DDummy); $a++)
{
echo '<b>'.$arrMenu[$a]['MenuName'][0][0].'</b><br>';
for ($b=0; $b < sizeof($arrDummy); $b++)
{
if ($arrDummy[$b]['Parent'] == $arr3DDummy[$a]['MenuName'][0][0])
{
//$arrMenu[$a][$L2]['Parent'][0] = $arrDummy[$b]['Parent'];
//$arrMenu[$a][$L2]['MenuName'][0] = $arrDummy[$b]['MenuName'];
echo ' '.$arrDummy[$b]['MenuName'].'<br>';
for ($c=0; $c < sizeof($arrDummy); $c++)
{
if ($arrDummy[$c]['Parent'] == $arrDummy[$b]['MenuName'])
{
echo ' '.$arrDummy[$c]['MenuName'].'<br>';
}
}
$L2++;
}
}
}
This displays the menu items in an hierarchical fasion. But it takes WAY to loong. Is there an more effective way of doing this?
My second problem is that I can't get the menu items stored in the 3D array. thw way I do in doesn't work.
All variables you see here is defined - I just haven't cut'n'past everything.
I hope someone out there is able to help me 🙂
Cheers