Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Klasa dynamicznego menu
Forum PHP.pl > Forum > PHP
marcinpruciak
Napisałem taki kod:

  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2.  
  3. /*
  4.  * @author Administrator
  5.  * @filename Menu.php
  6.  * @date 2009-11-21
  7.  */
  8.  
  9. class Side_Menu_Core
  10. {
  11. protected $type; //guest, user, admin
  12. protected $items;
  13. static $instance = NULL;
  14.  
  15. private function __construct($type = null) {
  16. $this->type = $type;
  17.  
  18. /*
  19.   * pobranie z bazy stałych elementów to chyba dobre miejsce
  20.   */
  21. }
  22.  
  23. public static function instance($type = null){
  24. if (self::$instance === NULL)
  25. self::$instance = new Side_Menu($type);
  26. self::$instance->type = $type;
  27. return self::$instance;
  28. }
  29.  
  30. public function add(Menu_item $item){
  31. $this->items[$item->id] = $item;
  32. if($item->parent > 0){
  33. $this->items[$item->parent]->append($item->id);
  34. }
  35. print Kohana::debug($this->items);
  36. }
  37.  
  38. public function get($id){
  39. return $this->items[$id];
  40. }
  41.  
  42. public function set_active($id){
  43. if($id == 0){
  44. return;
  45. }
  46. $item = $this->get($id);
  47. $item->active = TRUE;
  48. $this->set_active($item->parent);
  49. }
  50.  
  51. public function __toString() {
  52. return $this->render();
  53. }
  54.  
  55. public function render(){
  56. $output = '<ul>';
  57. foreach($this->items as $item){
  58. $output .= $item->render();
  59. }
  60. $output .= '</ul>';
  61.  
  62. return $output;
  63. }
  64.  
  65. public function get_parents(){
  66.  
  67. }
  68.  
  69. public function get_children($id_parent){
  70.  
  71. }
  72. }
  73.  
  74. class Menu_item
  75. {
  76. public $id = 0;
  77. public $parent = 0;
  78. public $children;
  79. public $title;
  80. public $link;
  81. public $active;
  82.  
  83. public function __construct($setting) {
  84. if(!empty($setting['parent'])){
  85. $this->parent = $setting['parent'];
  86. }
  87. $this->title = $setting['title'];
  88. $this->link = $setting['link'];
  89. $this->id = $setting['id'];
  90. }
  91.  
  92. public function append($id){
  93. $this->children[] = $id;
  94. }
  95.  
  96. public function render(){
  97. $output = '';
  98. if (empty($this->children) && $this->parent != 0){
  99. return '<li>'.$this->title.'</li>';
  100. }
  101.  
  102. if(!empty($this->children)){
  103. $output .= '<li>'.$this->title.'</li>';
  104. $output .= '<ul>';
  105. foreach($this->children as $child){
  106. $output .= Side_Menu_Core::instance()->get($child)->render();
  107. }
  108. $output .= '</ul>';
  109. }
  110. return $output;
  111. }
  112.  
  113. public function render_without_children(){
  114. return $output = '<li>'.$this->title.'</li>';
  115. }
  116. }
  117. ?>


Mam problem, gdy oznaczę jeden wpis jako child, on dalej wyświetla się jako parent. Probowałem różnymi ifami, że parent = 0 empty(child), ale dalej wyświetla się drugi raz w głównej linii.

Przy okazji prosiłbym o uwagi to tej klasy.

darko
Jak poddajesz coś pod ocenę, to przynajmniej udostępnij samodzielny kod, dołącz klasę Side_Menu, a nie

Fatal error: Class 'Side_Menu' not found in /var/www/robocze/abcdefghijkl.php on line 25 :

  1. <?php // defined('SYSPATH') OR die('No direct access allowed.');
  2.  
  3. /*
  4.  * @author Administrator
  5.  * @filename Menu.php
  6.  * @date 2009-11-21
  7.  * @TODO brak opisu klasy, za co odpowiada
  8.  */
  9.  
  10. class Side_Menu_Core
  11. {
  12. protected $type; //guest, user, admin
  13. protected $items;
  14. static $instance = NULL;
  15.  
  16. private function __construct($type = null) {
  17. $this->type = $type;
  18.  
  19. /*
  20.   * pobranie z bazy stałych elementów to chyba dobre miejsce
  21.   *
  22.   * Jak najbardziej
  23.   */
  24. }
  25.  
  26. public static function instance($type = null) {
  27. if (self::$instance === NULL)
  28. self::$instance = new Side_Menu($type);
  29. self::$instance->type = $type;
  30. return self::$instance;
  31. }
  32.  
  33. public function add(Menu_item $item){
  34. $this->items[$item->id] = $item;
  35. if($item->parent > 0){
  36. $this->items[$item->parent]->append($item->id);
  37. }
  38. // no Kochana specific methods!
  39. //print Kohana::debug($this->items);
  40. }
  41.  
  42. public function get($id){
  43. return $this->items[$id];
  44. }
  45.  
  46. public function set_active($id){
  47. if($id == 0){
  48. return;
  49. }
  50. $item = $this->get($id);
  51. $item->active = TRUE;
  52. $this->set_active($item->parent);
  53. }
  54.  
  55. public function __toString() {
  56. return $this->render();
  57. }
  58.  
  59. public function render(){
  60. $output = '<ul>';
  61. foreach($this->items as $item){
  62. $output .= $item->render();
  63. }
  64. $output .= '</ul>';
  65.  
  66. return $output;
  67. }
  68.  
  69. public function get_parents(){
  70.  
  71. }
  72.  
  73. public function get_children($id_parent){
  74.  
  75. }
  76. }
  77.  
  78. class Menu_item
  79. {
  80. public $id = 0;
  81. public $parent = 0;
  82. public $children = array();
  83. public $title;
  84. public $link;
  85. public $active;
  86.  
  87. public function __construct($setting) {
  88. if(!empty($setting['parent'])) {
  89. $this->parent = $setting['parent'];
  90. }
  91. $this->title = $setting['title'];
  92. $this->link = $setting['link'];
  93. $this->id = $setting['id'];
  94. }
  95.  
  96. public function append($id) {
  97. $this->children[] = $id;
  98. }
  99.  
  100. public function render() {
  101. $output = '';
  102. if (empty($this->children) && $this->parent != 0) {
  103. return '<li>'.$this->title.'</li>';
  104. }
  105.  
  106. if(!empty($this->children)) {
  107. $output .= '<li>'.$this->title.'</li>';
  108. $output .= '<ul>';
  109. foreach($this->children as $child) {
  110. $output .= Side_Menu_Core::instance()->get($child)->render();
  111. }
  112. $output .= '</ul>';
  113. }
  114. return $output;
  115. }
  116.  
  117. public function render_without_children() {
  118. return $output = '<li>'.$this->title.'</li>';
  119. }
  120. }
  121. // to już próba utworzenia Twoich obiektów, niestety jw.
  122. $side_menu_core = Side_Menu_Core::instance();
  123. $menu_item = new Menu_item();
  124.  
  125. ?>


ps. nie lepiej użyć hierarchii obiektów, żeby sprawdzić, czy dany element jest rodzicem czy dzieckiem elementu wejściowego questionmark.gif
marcinpruciak
Dzięki za odpowiedź ale już sobie poradziłem.
Klasa Menu i Menu_Core to jest ta sama klasa. Jest to moja biblioteka z frameworka Kohana on sobie tak nazywa klasy. Więc kod działa. Jakbyś zmienił nazwę klasy na Side_Menu to powinno zadziałać samodzielnie.





Cysiaczek
Zero problematyki OOP. ->PHP
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.