I've been working on updating my site to include sub-albums, and I wanted a nested list to be able to navigate to all the albums quickly. There doesn't seem to be any neat way to do this with the code that's there, so I wrote my own function to do this. You can see a work in progress here:
http://wp.tuxable.com/exploration/It's not very efficient and very hackish, so if anyone has ideas to improve it, please post. Otherwise feel free to use it.
Part of the problem with the code is that it's doing a mysql query for every iteration which isn't very efficient (it is a recursive function). Right now I've kept it from displaying the root galleries.
`// $parent is the parent of the children we want to see
// $level is increased when we go deeper into the tree, used to display a nice indented tree
// $ul_id is the id assigned to the first ul element.
function printGalleryTree($parent, $level, $ul_id) {
$sub_ul_id = null;
// retrieve all children of $parent
if(is_null($parent)){
$sql = "SELECT id, title, folder, parentid FROM ".prefix("albums")." WHERE parentid IS NULL";
}else{
$sql = "SELECT id, title, folder, parentid FROM ".prefix("albums")." WHERE parentid = '".$parent."'";
}
$result = query($sql);
if(($level > 0 || !is_null($parent)) && mysql_num_rows($result) != 0){
print "
if(isset($ul_id)){
print "id="".$ul_id.""";
}
print ">";
} else {
$sub_ul_id = $ul_id;
}
while ($row = mysql_fetch_array($result)) {
if($level == 0 && !is_null($parent)){
print "- ";
}elseif($level == 1){
print " - ";
}elseif($level == 2){
print " - ";
}elseif($level == 3){
print " - ";
}
if($level > 0 || !is_null($parent)){
//display each child
echo '';
echo $row['title'];
echo '';
}
// call this function again to display this child's children
printGalleryTree($row['id'], $level+1, $sub_ul_id);
$sub_ul_id = $sub_ul_id."1";
if(($level == 0 && !is_null($parent)) || $level == 1 || $level == 2 || $level == 3){
print " n";
}
}
if(($level > 0 || !is_null($parent)) && mysql_num_rows($result) != 0){
print "
n";
}
}`
Joel
(edit, fixed the url)
Comments
It now only does one database query and no reading of the directory structure. (disk accesses are generally slow) I had to split it up into 3 functions. Here they are:
`function printGalleryTree($parentid, $ul_id) {
//Get all the albums
$sql = "SELECT id, title, folder, parentid FROM ".prefix("albums");
$result = query($sql);
$albums = array();
while($row = mysql_fetch_array($result)){
array_push($albums, $row);
}
printGalleryTreeItems($albums, $parentid, 0, $ul_id, true);
}`
`function getChildren($albums, $id){
foreach($albums as $album){
if($album['parentid'] == $id){
return true;
}
}
return false;
}`
`function printGalleryTreeItems($albums, $parent, $level, $ul_id){
$sub_ul_id = null;
$count = 1;
if($parent != null){
print "
- ";
- ";
";
";if(isset($ul_id)){
print " id=\"".$ul_id."\"";
}
print ">";
}else{
$sub_ul_id = $ul_id;
}
foreach($albums as $album){
if($album['parentid'] == $parent){
if($level == 0 && !is_null($parent)){
print "
}elseif($level > 0){
print "
}
if($level > 0 || !is_null($parent)){
//display each child
print "";
print $album['title'];
print "";
}
$children = getChildren($albums, $album['id']);
if($children){
printGalleryTreeItems($albums, $album['id'], $level+1, $sub_ul_id, true);
}
if(isset($sub_ul_id)){
$sub_ul_id = $sub_ul_id.$count;
$count++;
}
if(($level == 0 && !is_null($parent)) || $level > 0){
print "
}
}
}
if($parent != null){
print "
}
}`
Hope this is useful for someone.
Joel