Recursive reading of flat file -> hierarchical menu?
Posted: Tue Apr 03, 2007 5:00 pm
I've managed to create a hierarchical tree-style menu based on recursively pulling data from a mysql database... but I've now discovered I may have to use a flat file rather than a database to hold the data that will create the menu - and I'm not sure how to approach that, having never used a flat text file as a data source before.
So, I'm just wondering what the best approach is, and if I could get some pointers what I should be looking at?
What I'm going to have is a list of courses, which I will divide into groups, with books assigned to each course. So, the data for each record will be:
Course Group
Course
Book Title
ISBN #
There will be one ISBN for each book, multiple books for each course, and multiple courses for each course group.
The menu is in the format of:
Course Group
- Course
- Book, ISBN
- Course
- Book, ISBN
- Book, ISBN
Course Group
- Course
- Book, ISBN
and so on. You can see the one that's based on the mysql database here: http://store.multipleangles.com/indexwmenu.php -- it's the ITP Course Books menu on the right.
I want have it set up in a way similar to how I've done it with the database, where the menu module is already coded and the data just needs to be loaded in, this time from a text file I can replace when the list is updated by the school.
Here's the code I'm using with the mysql database to create the menu (neither elegant nor efficient, I suspect, but it works):
So what I need to do now is get from a text file with the data, through something like this:
...recursively going through the text file to end up with something like this:
So, I assume this is possible, but I don't know where to begin.
I guess I need to know:
-- what's the best way to have the text file set up? should it be an xml file? or would there be a way to read the data from a more basic text file?
-- what should I be looking for in terms of available functions and areas in manuals?
-- any specific hints that will make this easier?
Thanks,
Scott
So, I'm just wondering what the best approach is, and if I could get some pointers what I should be looking at?
What I'm going to have is a list of courses, which I will divide into groups, with books assigned to each course. So, the data for each record will be:
Course Group
Course
Book Title
ISBN #
There will be one ISBN for each book, multiple books for each course, and multiple courses for each course group.
The menu is in the format of:
Course Group
- Course
- Book, ISBN
- Course
- Book, ISBN
- Book, ISBN
Course Group
- Course
- Book, ISBN
and so on. You can see the one that's based on the mysql database here: http://store.multipleangles.com/indexwmenu.php -- it's the ITP Course Books menu on the right.
I want have it set up in a way similar to how I've done it with the database, where the menu module is already coded and the data just needs to be loaded in, this time from a text file I can replace when the list is updated by the school.
Here's the code I'm using with the mysql database to create the menu (neither elegant nor efficient, I suspect, but it works):
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 itpgroups",$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 itpcourses 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))
{
$book=$myrow3['title'];
echo "<li><a href=\"http://astore.amazon.com/itp-ma4cs-20/detail/".$myrow3['isbn']."\" target=\"iframe\" title=\"".$book."\">".truncate_long_string($book, 22)."</a></li>";
}
echo "</li></ul>";
}
echo "</li></ul>";
}
echo "</li></ul>";
?>Code: Select all
<ul id="treemenu1" class="treeview">
<li>$CourseGroup
<ul>
<li>$CourseName
<ul>
<li><a href="http://url/$ISBN" target="iframe" title="$Title">truncate_long_string($Title, 20)</a></li>
</ul>
</li>
</ul>
</li>
</ul>Code: Select all
<ul id="treemenu1" class="treeview">
<li>Course Group 1
<ul>
<li>Course 1
<ul>
<li><a href="http://url/[ISBN]" target="iframe" title="[Title]">[shortened Title]...</a></li>
<li><a href="http://url/[ISBN]" target="iframe" title="[Title]">[shortened Title]...</a></li>
</ul>
</li>
<li>Course 2
<ul>
<li><a href="http://url/[ISBN]" target="iframe" title="[Title]">[shortened Title]</a></li>
<li><a href="http://url/[ISBN]" target="iframe" title="[Title]">[shortened Title]...</a></li>
</ul>
</li>
</ul>
</li>
<li>Courses Group 2
<ul>
<li>Course 3
<ul>
<li><a href="http://url/[ISBN]" target="iframe" title="[Title]">[shortened Title]...</a></li>
<li><a href="http://url/[ISBN]" target="iframe" title="[Title]">[shortened Title]...</a></li>
</ul>
</li>
<li>Course 4
<ul>
<li><a href="http://url/[ISBN]" target="iframe" title="[Title]">[shortened Title]...</a></li>
</ul>
</li>
<li>Course 5
<ul>
<li><a href="http://url/[ISBN]" target="iframe" title="[Title]">[shortened Title]...</a></li>
</ul>
</li>
</ul>
</li>
<li>Course Group 3
<ul>
<li>Course 6
<ul>
<li><a href="http://url/[ISBN]" target="iframe" title="[Title]">[shortened Title]...</a></li>
<li><a href="http://url/[ISBN]" target="iframe" title="[Title]">[shortened Title]...</a></li>
</ul>
</li>
</ul>
</li>
</ul>
I guess I need to know:
-- what's the best way to have the text file set up? should it be an xml file? or would there be a way to read the data from a more basic text file?
-- what should I be looking for in terms of available functions and areas in manuals?
-- any specific hints that will make this easier?
Thanks,
Scott