User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 363,776 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,476 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Mar 14th, 2006
Views: 3,831
menuWerx is a PHP class to generate a menu for your site. Options include: debug mode, horizontal or vertical orientation & hooks, optional inline styles, and strict or transitional modes. See an example at: http://www.Clear-Mind.com/menuwerx/
php Syntax
  1. //*********************************************************************************************
  2. menuwerx.php: (example of implementation)
  3. <?php
  4. include('menuwerx.class.php');
  5. $menu = new menuWerx();
  6. ?>
  7.  
  8.  
  9. //*********************************************************************************************
  10. menuwerx.config.php:
  11. <?php
  12. /*
  13. * menuWerx - Menu creation class for PHP
  14. * version: 1.0a
  15. * File: menuwerx.config.php
  16. * Liscense: GNU - http://www.gnu.org/copyleft/gpl.html
  17. * Author: Heath Nail
  18. * Website: http://www.Clear-Mind.com
  19. * Email: heathnail@hotmail.com
  20. */
  21. // Configuration settings
  22.  
  23. $this->aSettings['debug'] = false; // true to debug "UGLY!!", false for production sites
  24. $this->aSettings['orientation'] = 'horizontal'; // 'horizontal' or 'vertical' CSS can be edited below
  25. $this->aSettings['useInlineStyle'] ='yes'; // 'yes' turns on inline styles, 'no' disables inline styles
  26. // 'strict' disables use of target attribute
  27. // 'transitional' enables use of target attribute
  28. $this->aSettings['mode'] = 'strict';
  29.  
  30. // Menu details.
  31. // First page. The first or leftmost menu item should be page[1]!
  32.  
  33. $this->pages[1]['link'] = "menuwerx.php"; // filename
  34. $this->pages[1]['name'] = "Test Page 1"; // Name displayed on the menu
  35. $this->pages[1]['title'] = "The First Test Page"; // Title / Description of the link
  36. $this->pages[1]['target'] = "_top"; // _top opens in current window _blank opens in new window
  37. // for more info on target goto http://www.w3schools.com/tags/tag_a.asp
  38. // Second page
  39.  
  40. $this->pages[2]['link'] = "menuwerx2.php";
  41. $this->pages[2]['name'] = "Test Page 2";
  42. $this->pages[2]['title'] = "The Second Test Page";
  43. $this->pages[2]['target'] = "_top";
  44.  
  45. // Third page
  46.  
  47. $this->pages[3]['link'] = "menuwerx3.php";
  48. $this->pages[3]['name'] = "Test Page 3";
  49. $this->pages[3]['title'] = "The Third Test Page";
  50. $this->pages[3]['target'] = "_top";
  51.  
  52. // Fourth page
  53. /*
  54.   $this->pages[4]['link'] = "index4.php";
  55.   $this->pages[4]['name'] = "Test Page 4";
  56.   $this->pages[4]['title'] = "The Fourth Test Page";
  57.   $this->pages[4]['target'] = "_top";
  58. */
  59. // Fifth page
  60. /*
  61.   $this->pages[5]['link'] = "index5.php";
  62.   $this->pages[5]['name'] = "Test Page 5";
  63.   $this->pages[5]['title'] = "The Fifth Test Page";
  64.   $this->pages[5]['target'] = "_top";
  65. */
  66.  
  67. // 'vertical' css check out listamatic for cool ways to
  68. // style your lists http://css.maxdesign.com.au/listamatic/vertical08.htm
  69. $this->css['vertical'] = "
  70. <style>
  71. #menuWerx { width: 200px; }
  72.  
  73. #menuWerx ul
  74. {
  75. margin-left: 0;
  76. padding-left: 0;
  77. list-style-type: none;
  78. font-family: Arial, Helvetica, sans-serif;
  79. }
  80.  
  81. #menuWerx a
  82. {
  83. display: block;
  84. padding: 3px;
  85. width: 160px;
  86. background-color: #036;
  87. border-bottom: 1px solid #eee;
  88. }
  89.  
  90. #menuWerx a:link, #menuWerx-list a:visited
  91. {
  92. color: #EEE;
  93. text-decoration: none;
  94. }
  95.  
  96. #menuWerx a:hover
  97. {
  98. background-color: #369;
  99. color: #fff;
  100. }
  101.  
  102. #active a
  103. {
  104. background-color: #369;
  105. }
  106. </style>
  107. ";
  108.  
  109. // 'horizontal' css check out listamatic for cool ways to
  110. // style your lists http://css.maxdesign.com.au/listamatic/horizontal03.htm
  111. $this->css['horizontal'] = "
  112. <style>
  113. #menuWerx ul
  114. {
  115. padding-left: 0;
  116. margin-left: 0;
  117. background-color: #036;
  118. color: White;
  119. float: left;
  120. width: 100%;
  121. font-family: arial, helvetica, sans-serif;
  122. }
  123.  
  124. #menuWerx ul li { display: inline; }
  125.  
  126. #menuWerx ul li a
  127. {
  128. padding: 0.2em 1em;
  129. background-color: #036;
  130. color: White;
  131. text-decoration: none;
  132. float: left;
  133. border-right: 1px solid #fff;
  134. }
  135.  
  136. #menuWerx ul li a:hover
  137. {
  138. background-color: #369;
  139. color: #fff;
  140. }
  141.  
  142. #menuWerx ul li#active a
  143. {
  144. background-color: #369;
  145. }
  146. </style>
  147. ";
  148. ?>
  149.  
  150. //*********************************************************************************************
  151. menuwerx.class.php:
  152. <?php
  153. /*
  154. * menuWerx - Menu creation class for PHP
  155. * version: 1.0a
  156. * File: menuwerx.class.php
  157. * Liscense: GNU - http://www.gnu.org/copyleft/gpl.html
  158. * Author: Heath Nail
  159. * Website: http://www.Clear-Mind.com
  160. * Email: heathnail@hotmail.com
  161. */
  162. class menuWerx
  163. {
  164. //var $aSettings;
  165.  
  166. function menuWerx()
  167. {
  168. // Initialization functions - please do not edit unless you know
  169. // what you are doing.
  170.  
  171. include('menuwerx.config.php');
  172. $this->getActiveLink();
  173. if($this->active_link != "")
  174. {
  175. foreach($this->pages as $key => $page)
  176. {
  177. if($page['link'] == $this->active_link)
  178. {
  179. $this->pages[$key]['active'] = true;
  180. }
  181. }
  182. }
  183. $this->pages = $this->positionHori($this->pages);
  184. $this->pages = $this->makeURLs($this->pages);
  185. $this->pages = $this->wrapLI($this->pages);
  186. $this->pages = $this->wrapUL($this->pages);
  187. $this->pages = $this->wrapDiv($this->pages);
  188. $this->pages = $this->addStyle($this->pages);
  189. $this->showMenu();
  190. }
  191.  
  192. // Debug function - just echos values...
  193.  
  194. function deBug($id = "", $value)
  195. {
  196. if($this->aSettings['debug'] == true)
  197. {
  198. if(is_array($value) || is_object($value))
  199. {
  200. echo('<pre>');
  201. echo("Debug info: $id \n");
  202. print_r($value);
  203. echo('</pre>');
  204. }
  205. else
  206. {
  207. echo("Debug info: $id \n");
  208. echo($value);
  209. echo('<br>');
  210. }
  211. }
  212. }
  213.  
  214. // Determines the active page
  215.  
  216. function getActiveLink()
  217. {
  218. $this->script = getenv('SCRIPT_NAME');
  219. $this->script_exp = explode('/', $this->script);
  220. $this->script_exp_cnt = count($this->script_exp) - 1;
  221. $this->active_link = $this->script_exp[$this->script_exp_cnt];
  222. }
  223.  
  224. // Determines the horizontal or vertical position of the menu li elements. This
  225. // position is determined by the order of the array. 1st element is
  226. // 'far_left' or 'top', in between elements are 'between', and last element is
  227. // 'far_right' or 'bottom'. The value is stored in the ['position'] index.
  228.  
  229. function positionHori($pages_array)
  230. {
  231. $num_of_pages = count($pages_array);
  232. foreach($pages_array as $key => $page)
  233. {
  234. if($key == 1 && $this->aSettings['orientation'] == "horizontal")
  235. {$pages_array[$key]['position'] = 'far_left';}
  236. if($key == 1 && $this->aSettings['orientation'] == "vertical")
  237. {$pages_array[$key]['position'] = 'top';}
  238. if($key != 1 && $key != $num_of_pages)
  239. {$pages_array[$key]['position'] = 'between';}
  240. if($key == $num_of_pages && $this->aSettings['orientation'] == "horizontal")
  241. {$pages_array[$key]['position'] = 'far_right';}
  242. if($key == $num_of_pages && $this->aSettings['orientation'] == "vertical")
  243. {$pages_array[$key]['position'] = 'bottom';}
  244. }
  245. $this->deBug("position", $pages_array);
  246. return $pages_array;
  247. }
  248.  
  249. // Takes pages array and adds ['URL'] index with the actual URL as
  250. // the value
  251.  
  252. function makeURLs($pages_array)
  253. {
  254. if($this->aSettings['mode'] == 'transitional')
  255. {
  256. foreach($pages_array as $key => $page)
  257. {
  258. $pages_array[$key]['URL'] = '<a title="' . $page['title'] .
  259. '" target="' . $page['target'] .
  260. '" href="' . $page['link'] .'">' .
  261. $page['name'] . '</a>';
  262. }
  263. $this->deBug("makeURLs", $pages_array);
  264. return $pages_array;
  265. }
  266.  
  267. if($this->aSettings['mode'] == 'strict')
  268. {
  269. foreach($pages_array as $key => $page)
  270. {
  271. $pages_array[$key]['URL'] = '<a title="' . $page['title'] .
  272. '" href="' . $page['link'] .'">' .
  273. $page['name'] . '</a>';
  274. }
  275. $this->deBug("makeURLs", $pages_array);
  276. return $pages_array;
  277. }
  278. }
  279.  
  280. // Wraps the pages array ['URL'] with li tags and creates another index
  281. // of ['li']
  282.  
  283. function wrapLI($pages_array)
  284. {
  285. foreach($pages_array as $key => $page)
  286. {
  287. if($page['position'] == "between" && !isset($page['active']))
  288. {
  289. $pages_array[$key]['li'] = '<li>' . $page['URL'] . '</li>' . "\n";
  290. }
  291. if($page['position'] == "between" && isset($page['active']))
  292. {
  293. $pages_array[$key]['li'] = '<li id="active">' . $page['URL'] . '</li>' . "\n";
  294. }
  295. // Left
  296. if($page['position'] == "far_left" && !isset($page['active']))
  297. {
  298. $pages_array[$key]['li'] = '<li class="far_left">' . $page['URL'] . '</li>' . "\n";
  299. }
  300. if($page['position'] == "far_left" && isset($page['active']))
  301. {
  302. $pages_array[$key]['li'] = '<li id="active" class="far_left">' .
  303. $page['URL'] . '</li>' . "\n";
  304. }
  305. // Right
  306. if($page['position'] == "far_right" && !isset($page['active']))
  307. {
  308. $pages_array[$key]['li'] = '<li class="far_right">' . $page['URL'] . '</li>' . "\n";
  309. }
  310. if($page['position'] == "far_right" && isset($page['active']))
  311. {
  312. $pages_array[$key]['li'] = '<li id="active" class="far_right">' .
  313. $page['URL'] . '</li>' . "\n";
  314. }
  315. // Top
  316. if($page['position'] == "top" && !isset($page['active']))
  317. {
  318. $pages_array[$key]['li'] = '<li class="top">' . $page['URL'] . '</li>' . "\n";
  319. }
  320. if($page['position'] == "top" && isset($page['active']))
  321. {
  322. $pages_array[$key]['li'] = '<li id="active" class="top">' .
  323. $page['URL'] . '</li>' . "\n";
  324. }
  325. // Bottom
  326. if($page['position'] == "bottom" && !isset($page['active']))
  327. {
  328. $pages_array[$key]['li'] = '<li class="bottom">' . $page['URL'] . '</li>' . "\n";
  329. }
  330. if($page['position'] == "bottom" && isset($page['active']))
  331. {
  332. $pages_array[$key]['li'] = '<li id="active" class="bottom">' .
  333. $page['URL'] . '</li>' . "\n";
  334. }
  335. }
  336. $this->deBug("wrapLI", $pages_array);
  337. return $pages_array;
  338. }
  339.  
  340. // Wraps the menu in <ul> tags and returns the finished menu as a string.
  341.  
  342. function wrapUL($pages_array)
  343. {
  344. foreach($pages_array as $key => $page)
  345. {
  346. $li_st.= $page['li'];
  347. }
  348. $ul_st.= '<ul id="menuWerx-list">' . "\n";
  349. $ul_st.= $li_st;
  350. $ul_st.= '</ul>';
  351. $pages_array['ul'] = $ul_st;
  352.  
  353. $this->deBug("wrapUL", $pages_array['ul']);
  354. return $pages_array;
  355. }
  356.  
  357. // Wraps the menu in <div> tags and returns the finished menu as a string.
  358.  
  359. function wrapDiv($pages_array)
  360. {
  361. $pages_array['div'] = '<div id="menuWerx">' . "\n" . $pages_array['ul'] . "\n" . '</div>' . "\n";
  362. $this->deBug("wrapDiv", $pages_array['div']);
  363. return $pages_array;
  364. }
  365.  
  366. // Add the inline styles
  367.  
  368. function addStyle($pages_array)
  369. {
  370. if($this->aSettings['useInlineStyle'] != 'yes')
  371. {
  372. $pages_array['menu'] = "\n" . '<!--Begin menuWerx-->' . $pages_array['div'] . '<!--End menuWerx-->' . "\n\n";
  373. return $pages_array;
  374. }
  375. if($this->aSettings['orientation'] == 'vertical')
  376. {
  377. $pages_array['menu'] = "\n" . '<!--Begin menuWerx-->' . "\n" . $this->css['vertical'] . $pages_array['div'] . '<!--End menuWerx-->' . "\n\n";
  378. return $pages_array;
  379. }
  380. if($this->aSettings['orientation'] == 'horizontal')
  381. {
  382. $pages_array['menu'] = "\n" . '<!--Begin menuWerx-->' . "\n" . $this->css['horizontal'] . $pages_array['div'] . '<!--End menuWerx-->' . "\n\n";
  383. return $pages_array;
  384. }
  385. }
  386.  
  387. // Echos the menu for the browser
  388.  
  389. function showMenu()
  390. {
  391. echo($this->pages['menu']);
  392. }
  393.  
  394. }
  395.  
  396. ?>
  397.  
  398. //*********************************************************************************************
  399. menuwerx.php: (example of implementation)
  400. <?php
  401. include('menuwerx.class.php');
  402. $menu = new menuWerx("index.php", "hori");
  403. ?>
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 10:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC