Changeset 1099 for trunk/common
- Timestamp:
- 12/28/09 14:24:37 (2 years ago)
- Location:
- trunk/common
- Files:
-
- 2 edited
-
db/tags.php (modified) (1 diff)
-
tags.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/common/db/tags.php
r1089 r1099 60 60 // Return an array with id's of their parents 61 61 // Each parent id appears only once 62 function tag_get_parents($tag s) {62 function tag_get_parents($tag_ids) { 63 63 $query = sprintf("SELECT DISTINCT(`parent`) 64 64 FROM ia_tags 65 WHERE `id` IN (%s) ",66 implode(",", array_map('db_quote', $tag s))65 WHERE `id` IN (%s) AND `parent` != 0", 66 implode(",", array_map('db_quote', $tag_ids)) 67 67 ); 68 68 $result = db_fetch_all($query); 69 $ret = Array();69 $ret = array(); 70 70 foreach ($result as $row) { 71 71 $ret[] = $row['parent']; -
trunk/common/tags.php
r1089 r1099 52 52 } 53 53 54 // Receives a list of parent tags and children tags 55 // For each parent_tag adds an array 'sub_tags' containing 56 // an array with all his children tags 57 function build_tags_tree($parent_tags, $sub_tags) { 58 log_assert(is_array($parent_tags), "Parent tags is not an array"); 59 log_assert(is_array($sub_tags), "Children tags is not an array"); 54 // Receives a list of tags which will be arranged in a tree-like structure 55 // according to their respective parents. 56 function tag_build_tree($tags) { 57 log_assert(is_array($tags), "tag_build_tree expects an array of tags"); 58 $tags_by_id = array(); 59 // Group tags by id 60 foreach ($tags as &$tag) { 61 log_assert(is_tag($tag), "tag_build_tree expects an array of tags"); 62 log_assert(isset($tag["id"]), 63 "tag_build_tree expects an array of tags with id"); 60 64 61 $parent_tags_key = Array(); 62 foreach ($parent_tags as $key => $tag) { 63 $parent_tags[$key]['sub_tags'] = Array(); 64 $parent_tags_key[$tag['id']] = $key; 65 $tags_by_id[$tag["id"]] =& $tag; 65 66 } 66 67 67 foreach ($sub_tags as $tag) { 68 log_assert(isset($parent_tags_key[$tag['parent']]), "Child tag doesn't have a parent"); 69 $parent_tag_key = $parent_tags_key[$tag['parent']]; 70 $parent_tags[$parent_tag_key]['sub_tags'][] = $tag; 68 // Add 'sub_tags' attribute to all tags which have children. 69 $has_parent = array(); 70 foreach ($tags as &$tag) { 71 if (array_key_exists($tag["parent"], $tags_by_id)) { 72 $has_parent[$tag["id"]] = true; 73 74 if (!isset($tags_by_id[$tag["parent"]]["sub_tags"])) { 75 $tags_by_id[$tag["parent"]]["sub_tags"] = array(); 76 } 77 $tags_by_id[$tag["parent"]]["sub_tags"][] =& $tag; 78 } 71 79 } 72 80 73 return $parent_tags; 81 // Return only tree roots (tags which have no parents). 82 $roots = array(); 83 foreach ($tags_by_id as &$tag) { 84 if (!isset($has_parent[$tag["id"]])) { 85 $roots[] = $tag; 86 } 87 } 88 return $roots; 74 89 } 75 90
Note: See TracChangeset
for help on using the changeset viewer.
![[infoarena] development](/chrome/site/logo.png)