|
Posted by Rik on 07/16/07 18:29
On Mon, 16 Jul 2007 13:21:39 +0200, Jim <jimyt@yahoo.com> wrote:
> I understand what you're saying but when you consider that each time I=
> display a nav I need to execute a recursive function which may result
> in many calls to the database then I struggle to believe that it'll be=
> anywhere near as quick as retrieving an array from shared memory, even=
> if the entire database if cached....I'll have to perform some tests.
It should not have to be like that. If you have an adjacency model, what=
=
about this (bogus code, unchecked):
$navs =3D mysql_query('SELECT id, name, parent FROM table');
$pages =3D array();
while($page =3D mysql_fetch_assoc($navs)){
$pages[$page['id']] =3D $page;
}
foreach($pages as $id =3D> $page){
//Are 'root'-nodes with parent =3D 0 or NULL? both will be taken into =
account:
$parent =3D ($page['parent'] > 0) ? $page['parent'] : 0;
//may be unneccesary, but I like to be straight:
if(!isset($pages[$parent]['childs']) $pages[$parent]['childs'] =3D arra=
y();
//reference it in the parent:
$pages[$parent]['childs'][] =3D& $pages[$id];
}
print_r($pages[0]['childs']);
1 query, some fiddling with references, and you're done, be very wary fo=
r =
recursion in your tree though.
Offcourse, you could always try a nested set, might be more appropriate =
=
for a navigation:
http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
>> > and it feels like I'm making the db work un-
>> > necessarily when the data could be cached.
>>
>> Cross purposes. The db is working from cached data. Why layer another=
>> caching system over the top of that?
>
> Because there's a fair amount of php code executed to build the multi-=
> dimensional arrays that represent the site structure and data
> dictionary. It would save the execution of this code every time.
Possibly go for the simple solution of saving the tree once on changes i=
n =
the backend with var_export(), and just calling that from somewhere (fil=
e, =
db, etc)?
However, as others have said, only employ this kind of thing when you =
think your server isn't coping right now/takes to long to complete a pag=
e. =
If it's allright without chaching, don't bother with it.
-- =
Rik Wasmus
[Back to original message]
|