Changes between Version 1 and Version 2 of Howto/Navigation/HtmlMenu

Show
Ignore:
Author:
demian (IP: 212.158.235.212)
Timestamp:
03/02/06 06:16:14 (5 years ago)
Comment:

--

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 = 
    22 
    33[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 :-/] 
    5454 
    5555{{{ 
    56 #!php 
    57 <?php 
    5856require_once('HTML/Menu.php'); 
    5957require_once('DB/NestedSet.php'); 
    6058require_once('DB/NestedSet/Output.php'); 
    6159 
    62 '' We start out by defining our database table specifications 
     60// We start out by defining our database table specifications 
    6361$dsn = 'mysql://root:@localhost/nested'; 
    6462 
    65 '' next we let the point our the table fields to the expected fields 
     63// next we let the point our the table fields to the expected fields 
    6664$table = array( 
    6765    'id'        => 'id', 
    7573); 
    7674 
    77 '' And then create a NestedSet instance using the DB driver with our Database and Table 
     75// And then create a NestedSet instance using the DB driver with our Database and Table 
    7876 
    7977$nestedSet =& DB_NestedSet::factory('DB', $dsn, $table);  
    8078 
    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. 
    8381$nestedSet->setAttr(array( 
    8482        'node_table' => 'nested_set',  
    8785        ]; 
    8886         
    89 '' If a nodeID is specified we print out the name and title 
     87// If a nodeID is specified we print out the name and title 
    9088if(isset($_GET['nodeID']]{ 
    9189    $node_data = $nestedSet->pickNode($_GET['nodeID']); 
    9391} 
    9492 
    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 if 
    97 '' 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. 
    9896 
    99 '' Create a parent item 
     97// Create a parent item 
    10098 
    10199$parent = $nestedSet->createRootNode(array('name'  => "Menu", 
    102100                                           'title' => "Example Menu"), false, true); 
    103101 
    104 '' The $parent var now holds the ID of the root node, we will use this when  
    105 '' createing subnodes 
     102// The $parent var now holds the ID of the root node, we will use this when  
     103// createing subnodes 
    106104 
    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 this 
    109 '' node 
     105// 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 
    110108$italian = $nestedSet->createSubNode($parent, array('name'  => 'Italian', 
    111109                                                    'title' => 'Great Food'];                                                       
    122120$nestedSet->createSubNode($pizza, array('name' => 'Quattro Stagioni'];     
    123121     
    124 '' Now create some leaf nodes of our indian recipes 
     122// Now create some leaf nodes of our indian recipes 
    125123$nestedSet->createSubNode($indian, array('name' =>'Butter Chicken']; 
    126124$nestedSet->createSubNode($indian, array('name' =>'Tandoori Chicken']; 
    127125$nestedSet->createSubNode($indian, array('name' =>'Masala Dosa']; 
    128126 
    129 '' And Greek recipes 
     127// And Greek recipes 
    130128 
    131129$nestedSet->createSubNode($greek, array('name' => 'Moussaka']; 
    132130 
    133 '' Now our structure is created, if you look in the database you'll be able 
    134 '' to see how the DB_NestedSet stores this information. The next step is to 
    135 '' retrieve the data and present it in a pretty format. From now on we will  
    136 '' 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. 
    137135 
    138 '' Fecth the entire tree into an array 
     136// Fetch the entire tree into an array 
    139137$data = $nestedSet->getAllNodes(true); 
    140138 
    141 '' Create the links 
     139// Create the links 
    142140foreach ($data as $id => $node) { 
    143141     $data[$id]['url'] = $_SERVER['PHP_SELF'].'?nodeID=' . $node['id']; 
    144142} 
    145143 
    146 '' Send HTML_Menu the structure, title and URL for the items in our tree 
     144// Send HTML_Menu the structure, title and URL for the items in our tree 
    147145$params = array( 
    148146    'structure' => $data, 
    151149 
    152150     
    153 '' Create the output driver object     
     151// Create the output driver object     
    154152 
    155153$output =& DB_NestedSet_Output::factory($params, 'Menu'); 
    156154 
    157 '' Fetch the menu array 
     155// Fetch the menu array 
    158156$structure = $output->returnStructure(); 
    159157 
    160 '' We'll create the new HTML_Menu object, using the 'sitemap' display driver 
     158// We'll create the new HTML_Menu object, using the 'sitemap' display driver 
    161159$menu = & new HTML_Menu($structure, 'sitemap'); 
    162160 
    163 '' Create the current URL and send it to HTML_Menu 
     161// Create the current URL and send it to HTML_Menu 
    164162$currentUrl = $_SERVER['PHP_SELF'].'?nodeID=' . $_GET['nodeID']; 
    165163$menu->forceCurrentUrl($currentUrl); 
    166164 
    167 '' Show the menu 
     165// Show the menu 
    168166 
    169167$menu->show(); 
    170  
    171 ?> 
    172168}}}