Changes between Version 1 and Version 2 of Howto/Navigation/HtmlMenu
- Timestamp:
- 03/02/06 06:16:14 (5 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Howto/Navigation/HtmlMenu
v1 v2 1 FIXME: Look over new file... 1 = How To Create A Navigation Driver with PEAR's HTML_Menu = 2 2 3 3 [Taken from http://www.phpmag.net/itr/kolumnen/psecom,id,26,nodeid,207.html - sorry guys, there doesn't seem to be any direct link to the article :-/] 54 54 55 55 {{{ 56 #!php57 <?php58 56 require_once('HTML/Menu.php'); 59 57 require_once('DB/NestedSet.php'); 60 58 require_once('DB/NestedSet/Output.php'); 61 59 62 ''We start out by defining our database table specifications60 // We start out by defining our database table specifications 63 61 $dsn = 'mysql://root:@localhost/nested'; 64 62 65 ''next we let the point our the table fields to the expected fields63 // next we let the point our the table fields to the expected fields 66 64 $table = array( 67 65 'id' => 'id', 75 73 ); 76 74 77 ''And then create a NestedSet instance using the DB driver with our Database and Table75 // And then create a NestedSet instance using the DB driver with our Database and Table 78 76 79 77 $nestedSet =& DB_NestedSet::factory('DB', $dsn, $table); 80 78 81 ''Then set the names of our table, and how to sort the nodes.82 ''We have chosed to sort on 'name' but you can use any field from the table.79 // Then set the names of our table, and how to sort the nodes. 80 // We have chosed to sort on 'name' but you can use any field from the table. 83 81 $nestedSet->setAttr(array( 84 82 'node_table' => 'nested_set', 87 85 ]; 88 86 89 ''If a nodeID is specified we print out the name and title87 // If a nodeID is specified we print out the name and title 90 88 if(isset($_GET['nodeID']]{ 91 89 $node_data = $nestedSet->pickNode($_GET['nodeID']); 93 91 } 94 92 95 ''Now we define our data set. This only needs to be done one time.96 ''Once the information is in your database you will only need to touch it if97 ''You want to edit it.93 // Now we define our data set. This only needs to be done one time. 94 // Once the information is in your database you will only need to touch it if 95 // You want to edit it. 98 96 99 ''Create a parent item97 // Create a parent item 100 98 101 99 $parent = $nestedSet->createRootNode(array('name' => "Menu", 102 100 'title' => "Example Menu"), false, true); 103 101 104 ''The $parent var now holds the ID of the root node, we will use this when105 ''createing subnodes102 // The $parent var now holds the ID of the root node, we will use this when 103 // createing subnodes 106 104 107 ''Create a Subnode for our Italian recipes. We define the parent node first,108 ''and then capture the id of this node to use when creating subnodes from this109 ''node105 // Create a Subnode for our Italian recipes. We define the parent node first, 106 // and then capture the id of this node to use when creating subnodes from this 107 // node 110 108 $italian = $nestedSet->createSubNode($parent, array('name' => 'Italian', 111 109 'title' => 'Great Food']; 122 120 $nestedSet->createSubNode($pizza, array('name' => 'Quattro Stagioni']; 123 121 124 ''Now create some leaf nodes of our indian recipes122 // Now create some leaf nodes of our indian recipes 125 123 $nestedSet->createSubNode($indian, array('name' =>'Butter Chicken']; 126 124 $nestedSet->createSubNode($indian, array('name' =>'Tandoori Chicken']; 127 125 $nestedSet->createSubNode($indian, array('name' =>'Masala Dosa']; 128 126 129 ''And Greek recipes127 // And Greek recipes 130 128 131 129 $nestedSet->createSubNode($greek, array('name' => 'Moussaka']; 132 130 133 ''Now our structure is created, if you look in the database you'll be able134 ''to see how the DB_NestedSet stores this information. The next step is to135 ''retrieve the data and present it in a pretty format. From now on we will136 ''be working with HTML_Menu for the presentation.131 // Now our structure is created, if you look in the database you'll be able 132 // to see how the DB_NestedSet stores this information. The next step is to 133 // retrieve the data and present it in a pretty format. From now on we will 134 // be working with HTML_Menu for the presentation. 137 135 138 '' Fecth the entire tree into an array136 // Fetch the entire tree into an array 139 137 $data = $nestedSet->getAllNodes(true); 140 138 141 ''Create the links139 // Create the links 142 140 foreach ($data as $id => $node) { 143 141 $data[$id]['url'] = $_SERVER['PHP_SELF'].'?nodeID=' . $node['id']; 144 142 } 145 143 146 ''Send HTML_Menu the structure, title and URL for the items in our tree144 // Send HTML_Menu the structure, title and URL for the items in our tree 147 145 $params = array( 148 146 'structure' => $data, 151 149 152 150 153 ''Create the output driver object151 // Create the output driver object 154 152 155 153 $output =& DB_NestedSet_Output::factory($params, 'Menu'); 156 154 157 ''Fetch the menu array155 // Fetch the menu array 158 156 $structure = $output->returnStructure(); 159 157 160 ''We'll create the new HTML_Menu object, using the 'sitemap' display driver158 // We'll create the new HTML_Menu object, using the 'sitemap' display driver 161 159 $menu = & new HTML_Menu($structure, 'sitemap'); 162 160 163 ''Create the current URL and send it to HTML_Menu161 // Create the current URL and send it to HTML_Menu 164 162 $currentUrl = $_SERVER['PHP_SELF'].'?nodeID=' . $_GET['nodeID']; 165 163 $menu->forceCurrentUrl($currentUrl); 166 164 167 ''Show the menu165 // Show the menu 168 166 169 167 $menu->show(); 170 171 ?>172 168 }}}
