Hierarchial menu

T

Tore Aursand

Hi!

Like all other web sites, my homepage needs a menu. I've come to a
decision where I want to be able to link "all pages to all the other
pages", ie. a hierarchial structure where a child can have multiple
parents.

Everything is store inside a database, and here's the data structure,
where each item in the menu is a 'section' (as MySQL);

section
-------
section_id int unsigned auto_increment primary key,
title varchar(80) not null

section_parent
--------------
section_id int unsigned not null,
parent_id int unsigned not null,
position int unsigned not null

The 'position' field denotes the - right - position of the section
relative to its parent.

What I need is a function to gather the data about all the sections, what
"level" they are on etc., so that it's as easy as possible for me to
output the menu;

my $stSections = $dbh->prepare('SELECT s.section_id, s.title,
sp.parent_id, sp.position
FROM section s, section_parent sp
ORDER BY sp.parent_id, sp.position');
$stSections->execute();
while ( my @section = $stSections->fetchrow_array() ) {
my $section_id = $section[0];
my $title = $section[1];
my $parent_id = $section[2];
my $position = $section[3];
}
$stSections->finish();

How should I really proceed from here? What's the best way? I'm not sure
what to try from here...?

Thanks again for all help!


--
Tore Aursand <[email protected]>
"Anyone who slaps a 'this page is best viewed with Browser X'-label on
a web page appears to be yearning for the bad old days, before the
web, when you had very little chance of reading a document written on
another computer, another word processor or another network." -- Tim
Berners-Lee, July 1996
 
S

Sandman

Tore Aursand said:
Hi!

Like all other web sites, my homepage needs a menu. I've come to a
decision where I want to be able to link "all pages to all the other
pages", ie. a hierarchial structure where a child can have multiple
parents.

Everything is store inside a database, and here's the data structure,
where each item in the menu is a 'section' (as MySQL);

section
-------
section_id int unsigned auto_increment primary key,
title varchar(80) not null

section_parent
--------------
section_id int unsigned not null,
parent_id int unsigned not null,
position int unsigned not null

The 'position' field denotes the - right - position of the section
relative to its parent.

What I need is a function to gather the data about all the sections, what
"level" they are on etc., so that it's as easy as possible for me to
output the menu;

my $stSections = $dbh->prepare('SELECT s.section_id, s.title,
sp.parent_id, sp.position
FROM section s, section_parent sp
ORDER BY sp.parent_id, sp.position');
$stSections->execute();
while ( my @section = $stSections->fetchrow_array() ) {
my $section_id = $section[0];
my $title = $section[1];
my $parent_id = $section[2];
my $position = $section[3];
}
$stSections->finish();

How should I really proceed from here? What's the best way? I'm not sure
what to try from here...?

Instead of helping you take the next step, I'll tell you how I did it. The
problem with such a menu is that it need to be content aware, meaning the right
menu should be highlighted when you're at a specific place at the site. On a
dynamic site (which is the only sites I build) most pages you surf to are named
sometihng like "/texts/list.php?category=Vacation" which won't help much.

So, for every menu item, you need to pass along a set of rules for when it is
supposed to be active. I am writing my menu in PHP, but the code and the
general idea is the same in both cases. It looks something like this:


menu("Homepage", "/index.php", "^(\/|\/index.php.*?)$");
menu("FAQ", "/texts/list.php?category=FAQ", "category=FAQ");


Now, these are calls to a function that take at least three arguments. First
one being the label of the menu, next one being the page to which it should
link and the third one being a regular expression meant to be used on
$_SERVER["REQUEST_URI"] or $ENV{"REQUEST_URI"} if you're running perl (and are
running the perl script as ssi or cgi script, and run apache bla bla).

the menu funtion looks something like this:

function menu ($name, $link, $grep){
# which corresponds to something like
# sub menu {
# ($name, $link, $indent) = @_;
# }
$bg = "#ffffff" # default background color of menu

if (preg_match("/$grep/", $_SERVER["REQUEST_URI"])){
# if ($ENV{'REQUEST_URI'}=~m/$grep/) ?
# Yes, this is an active menu!
# so we'll set the background color or css class or whatever
# to something nice
$bg = "#eeeeee";
}

print "<tr><td bgcolor='$bg'>$name</td></tr>";
}


Something like that. Obviously my way required the menuitems to be called in
the order in which they should appaer, so if name, link and grep is kept in a
MySQL table, you'd have to sort it by section as you wer etalking about, and
perhpas have a section() function to output whenever the section changes, like
this:

$sql = $->prepare("select name, link, grep, section from menu order by
section");
$sql->execute;

while ($a = $sql->fetchrow_hashref()) {

# did the section change?
section($$a{'section'}) if $$a{'section'} ne $ssection;

menu($$a{'name'}, $$a{'link'}, $$a{'grep'});

# save the current section to the next iteration, to see it if changed.
$ssection = $$a{'section'};
}


Hope my ideas helped some, allthough I realize they weren't a clean-cut answer
to your question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top