Getting from MySQL table -> hierarchical navigation menu
Posted: Mon Apr 02, 2007 12:08 pm
feyd | Please use
Each 'coursegroup' will have several 'coursenum' and each 'coursenum' will have several 'title' and 'isbn' entries. I want to end up with a navigation menu that is like this:
Here's what I tried and got partially working:[/syntax]
This does get the nested hierarchy displayed, but it displays each of the coursegroups several times, so I've got something like this:
Unfortunately, I don't know enough to know which is the best approach, and would probably need help getting going if I'm to change strategies.
Suggestions?
Thanks,
Scott
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I've taken a stab at doing this myself, and it seems to be partially/almost working, but I can't get it to where I need it. I'm also not sure if the approach I've taken makes any sense...
I have set up a single db table as follows:
[syntax="sql"]CREATE TABLE `itpbooks` (
`id` int(11) NOT NULL auto_increment,
`coursegroup` varchar(15) default NULL,
`coursenum` varchar(100) NOT NULL default '',
`title` varchar(250) default NULL,
`isbn` varchar(15) default NULL,
KEY `id` (`id`)
)- Course Group 1
- - Course A
- - < a href to ISBN 1>Title 1< / a>
- - < a href to ISBN 2>Title 2< / a>
- - Course B
- - < a href to ISBN 3>Title 3< / a>
- - Course A
- Course Group 2
- - Course C
- - < a href to ISBN 4>Title 4< / a>
- - Course C
Here's what I tried and got partially working:[/syntax]
Code: Select all
<?php
$db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name,$db);
$result = mysql_query("SELECT coursegroup FROM itpbooks",$db);
echo "<ul id=\"treemenu1\" class=\"treeview\">";
while ($myrow = mysql_fetch_array($result))
{
echo "<li>".$myrow['coursegroup']."";
$grouping=$myrow['coursegroup'];
$result2 = mysql_query("SELECT coursenum FROM itpbooks WHERE coursegroup = '$grouping'",$db);
echo "<ul>";
while ($myrow2 = mysql_fetch_array($result2))
{
echo "<li>".$myrow2['coursenum']."";
$course=$myrow2['coursenum'];
$result3 = mysql_query("SELECT * FROM itpbooks WHERE coursenum = '$course'",$db);
echo "<ul>";
while ($myrow3 = mysql_fetch_array($result3))
{
echo "<li><a href=\"http://astore.amazon.com/itp-ma4cs-20/detail/".$myrow3['isbn']."\" target=\"iframe\">".$myrow3['title']."</a></li>";
}
echo "</li></ul>";
}
echo "</li></ul>";
}
echo "</li></ul>";
?>- Course Group 1
- - Course A
- - < a href to ISBN 1>Title 1< / a>
- - < a href to ISBN 2>Title 2< / a>
- - Course B
- - < a href to ISBN 3>Title 3< / a>
- - Course A
- Course Group 1
- - Course A
- - < a href to ISBN 1>Title 1< / a>
- - < a href to ISBN 2>Title 2< / a>
- - Course B
- - < a href to ISBN 3>Title 3< / a>
- - Course A
- Course Group 2
- - Course C
- - < a href to ISBN 4>Title 4< / a>
- - Course C
- Course Group 2
- - Course C
- - < a href to ISBN 4>Title 4< / a>
- - Course C
Unfortunately, I don't know enough to know which is the best approach, and would probably need help getting going if I'm to change strategies.
Suggestions?
Thanks,
Scott
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]