![]() | ||||||||||
![]() | ||||||||||
| ||||||||||
In addition to wide range of useful features CodeThatMenu allows you to generate menu script from the database. At the example below we use MySQL as database platform and PHP as web-script language. But you can choose your own favorite database platform and web-script language. Here we just try to show basic principles for database menu generation. First of all we create three tables - script_items, script_styles and scripts. Tables structure and data are listed below:
CREATE TABLE scripts ( id INT AUTO_INCREMENT NOT NULL, scriptname VARCHAR(255), PRIMARY KEY (id) ); insert into scripts values(1, 'CodeThatMenu Standard'); insert into scripts values(2, 'CodeThatTree Standard'); insert into scripts values(3, 'CodeThatCalendar Standard',); insert into scripts values(4, 'CodeThatXPBar Standard'); CREATE TABLE script_items ( item_id INT AUTO_INCREMENT NOT NULL, script_id INTEGER NOT NULL, item_name VARCHAR(255), style_id INTEGER DEFAULT 0, style_over_id INTEGER DEFAULT 0, item_parent_id INTEGER DEFAULT 0, PRIMARY KEY (item_id) ); CREATE TABLE script_styles ( style_id INT AUTO_INCREMENT NOT NULL, css VARCHAR(255), size VARCHAR(20), bgcolor VARCHAR(7), color VARCHAR(7), bgimg VARCHAR(255), shadow VARCHAR(255), border VARCHAR(255), imgitem VARCHAR(255), imgdir VARCHAR(255), imgendon VARCHAR(255), imgendoff VARCHAR(255), PRIMARY KEY (style_id) ); insert into script_items values(1, 1, 'Menu from the DataBase', 1, 2, 0); insert into script_items values(2, 1, 'Item 1', 0, 0, 1); insert into script_items values(3, 1, 'Item 2', 0, 0, 1); insert into script_items values(4, 1, 'Item 3', 0, 0, 1); insert into script_items values(5, 1, 'Item 2:1', 0, 0, 3); insert into script_items values(6, 1, 'Item 2:2', 0, 0, 3); insert into script_styles values(1, 'test2', '[160,16]', '#619DF5', '#ffffff', '', '{"color":"gray", "width":3}', '{"color":"#003882", "width":1}', '', '', '{"src" : "img/arr_down2.gif", "width":16,"height":16}', '{"src" : "img/arr_right2.gif", "width":16,"height":16}'); insert into script_styles values(2, 'test2', '', '#CCCCCC', '#000000', '', '', '', '', '', '', ''); Of course, when you create your real CodeThatMenu script, tables will contain more information. Next step - we create .php script. Result of this script implementation is a string variable that contains full menu definition:
function create_level($parent_id ) { $items = ''; $StrQuery="select item_id, item_name " . " from script_items " . " where script_id=1 and item_parent_id=" . $parent_id; $result=mysql_query($StrQuery); $Error = @mysql_error($db); if ($Error!="") { echo "$Error"; exit(); } $first = 1; while(list($item_id, $item_name) = mysql_fetch_row($result)) { if($first==1) $first=0; else $items.=","; $next_level = create_level($item_id); if($next_level == "") { $items.=("{\"text\":\"".$item_name."\""); $items.=$next_level; $items.="}"; } else { $items.=("{\"text\":\"".$item_name."\",\"menu\":{\"items\":["); $items.=$next_level; $items.="]}}"; } } return ($items); } function create_definition() { global $db; $definition = ""; $StrQuery="select distinct css, size, bgcolor, color, bgimg, shadow, border, imgitem, imgdir, " . " imgendon, imgendoff " . " from script_styles, script_items where script_styles.style_id = script_items.style_id " . " and script_items.item_parent_id=0 and script_id=1"; $result=mysql_query($StrQuery); $res_num=@mysql_numrows($result); $Error = @mysql_error($db); if ($Error!="") { echo "$Error"; exit(); } if ($res_num!=0) { $definition = $definition . 'var MenuDef = {"style" : {"css" : "' . @mysql_result($result, 0, "css") . '", ' . '"size":' . @mysql_result($result, 0, "size") . ', ' . '"bgcolor":"' . @mysql_result($result, 0, "bgcolor") . '", ' . '"color":"' . @mysql_result($result, 0, "color") . '", ' . '"shadow":' . @mysql_result($result, 0, "shadow") . ', ' . '"border":' . @mysql_result($result, 0, "border") . ', ' . '"imgendon":' . @mysql_result($result, 0, "imgendon") . ', ' . '"imgendoff":' . @mysql_result($result, 0, "imgendoff") . '}'; } $StrQuery="select distinct css, bgcolor, color, bgimg, shadow, border, imgitem, imgdir, " . " imgendon, imgendoff " . " from script_styles, script_items where script_styles.style_id = script_items.style_over_id " . " and script_items.item_parent_id=0 and script_id=1"; $result=mysql_query($StrQuery); $res_num=@mysql_numrows($result); $Error = @mysql_error($db); if ($Error!="") { echo "$Error"; exit(); } if ($res_num!=0) { $definition = $definition . ', "itemover": { "css" : "' . @mysql_result($result, 0, 'css') . '", ' . '"bgcolor":"' . @mysql_result($result, 0, 'bgcolor') . '", ' . '"color":"' . @mysql_result($result, 0, 'color') . '"} '; } $definition = $definition . ', "position": { "absolute": false, "pos":[30,20] } '; $items=', "items" : [ '; $items = $items . create_level(0); $items= $items . ' ] '; $definition = $definition . $items . " };"; return $definition; } And finally we complete a web page for menu script:
<? echo '<HEAD><link href="/common_codethat.css" rel="stylesheet" type="text/css"> ' . '<script language="javascript1.2" src="' . site_url . 'codethatsdk.js"></script> ' . '<script language="javascript1.2" src="' . site_url . 'menu/codethatmenustd.js"></script> ' . '<script language="javascript1.2"> ' . '' . create_definition() . '</script> </head>'; ?> <body bgcolor="#ffffff"> <script> var menuTest1 = new CMenu(MenuDef, 'menuTest1'); menuTest1.create(); menuTest1.run(); </script> </body> </html>
You can see an example and complete code here - Menu Generation from the Database Example Read more about CodeThatMenu >>
|