Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [CSS][MySQL][PHP]dynamicznie rozwijane MENU
Forum PHP.pl > Forum > Przedszkole
sidoke
Witam serdecznie, muszę zrobić dynamicznie rozwijane menu pobierane z bazy danych mysql. Tzn po kliknięciu rozwijają nam się podkategorie. Tak, jak np. na stronie http://fsmoto.pl/pl/katalizatory
Po kliknięciu w daną markę pojazdu rozwijają nam się odpowiednie modele. Pomoże ktoś?
MarcinKonewski
Naprawdę bardzo łatwo jest coś takiego znaleść w Internecie... Poszukać trochę. Jest pełno przykładów oraz gotowych skryptów.

https://www.google.pl/?gws_rd=ssl#q=mysql+menu

MySQL:
  1. CREATE TABLE menu_items(
  2. menu_item_id int NOT NULL AUTO_INCREMENT,
  3. menu_item_name tinytext,
  4. menu_description tinytext,
  5. menu_url text,
  6. menu_parent_id int NOT NULL DEFAULT 0,
  7. PRIMARY KEY(menu_item_id)
  8. ) ENGINE=INNODB;
  9.  
  10. INSERT INTO menu_items
  11. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  12. VALUES
  13. (1, 'Cars', 'Stuff about cars', '#', 0);
  14.  
  15. INSERT INTO menu_items
  16. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  17. VALUES
  18. (2, 'Sedans', 'Sedan cars', '#', 1);
  19.  
  20. INSERT INTO menu_items
  21. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  22. VALUES
  23. (3, 'Station Wagons', 'Station Wagon cars', '#', 1);
  24.  
  25. INSERT INTO menu_items
  26. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  27. VALUES
  28. (4, 'Utes', 'You Beaut', '#', 1);
  29.  
  30. INSERT INTO menu_items
  31. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  32. VALUES
  33. (5, 'Big Utes', 'You Beaut', '#', 4);
  34.  
  35. INSERT INTO menu_items
  36. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  37. VALUES
  38. (6, 'Little Utes', 'You Beaut', '#', 4);
  39.  
  40.  
  41. INSERT INTO menu_items
  42. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  43. VALUES
  44. (7, 'Trucks', 'Stuff about Trucks', '#', 0);
  45.  
  46. INSERT INTO menu_items
  47. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  48. VALUES
  49. (8, 'Tip Trucks', 'Tippers', '#', 7);
  50.  
  51. INSERT INTO menu_items
  52. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  53. VALUES
  54. (9, 'Lorries', 'Bigger trucks', '#', 8);
  55.  
  56. INSERT INTO menu_items
  57. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  58. VALUES
  59. (10, 'Road Trains', 'LOOONG Trucks', '#', 8);
  60.  
  61.  
  62. INSERT INTO menu_items
  63. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  64. VALUES
  65. (11, 'Boats', 'LOOONG Trucks', '#', 0);
  66.  
  67. INSERT INTO menu_items
  68. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  69. VALUES
  70. (12, 'Yachts', 'Sail Boats', '#', 11);
  71.  
  72. INSERT INTO menu_items
  73. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  74. VALUES
  75. (13, 'Power Boats', 'Fast Boats', '#', 11);
  76.  
  77. INSERT INTO menu_items
  78. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  79. VALUES
  80. (14, 'Dinghy', 'Small Power Boats', '#', 13);
  81.  
  82. INSERT INTO menu_items
  83. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  84. VALUES
  85. (15, 'Half Cabin', 'Bigger Boats', '#', 13);
  86.  
  87. INSERT INTO menu_items
  88. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  89. VALUES
  90. (16, 'Full Cabin', 'Even Bigger Boats', '#', 13);
  91.  
  92. INSERT INTO menu_items
  93. (menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
  94. VALUES
  95. (17, 'Cruisers', 'Really Big Boats', '#', 13);




Plik:
  1. <?php
  2.  
  3. // connect to mysql
  4. $link = mysql_connect( 'localhost', 'username', 'password' );
  5.  
  6. // select the database
  7. mysql_select_db('my_database', $link);
  8.  
  9.  
  10. // create an array to hold the references
  11. $refs = array();
  12.  
  13. // create and array to hold the list
  14. $list = array();
  15.  
  16. // the query to fetch the menu data
  17. $sql = "SELECT menu_item_id, menu_parent_id, menu_item_name FROM menu_items ORDER BY menu_item_name";
  18.  
  19. // get the results of the query
  20. $result = mysql_query($sql);
  21.  
  22. // loop over the results
  23. while($data = @mysql_fetch_assoc($result))
  24. {
  25. // Assign by reference
  26. $thisref = &$refs[ $data['menu_item_id'] ];
  27.  
  28. // add the the menu parent
  29. $thisref['menu_parent_id'] = $data['menu_parent_id'];
  30. $thisref['menu_item_name'] = $data['menu_item_name'];
  31.  
  32. // if there is no parent id
  33. if ($data['menu_parent_id'] == 0)
  34. {
  35. $list[ $data['menu_item_id'] ] = &$thisref;
  36. }
  37. else
  38. {
  39. $refs[ $data['menu_parent_id'] ]['children'][ $data['menu_item_id'] ] = &$thisref;
  40. }
  41. }
  42.  


Plik ciąg dalszy:
  1. function create_list( $arr )
  2. {
  3. $html = "\n<ul>\n";
  4. foreach ($arr as $key=>$v)
  5. {
  6. $html .= '<li>'.$v['menu_item_name']."</li>\n";
  7. if (array_key_exists('children', $v))
  8. {
  9. $html .= "<li>";
  10. $html .= create_list($v['children']);
  11. $html .= "</li>\n";
  12. }
  13. else{}
  14. }
  15. $html .= "</ul>\n";
  16. return $html;
  17. }
  18.  
  19. echo create_list( $list );
  20.  
  21. ?>
sidoke
Tak, ale tutaj wyświetla się całe drzewko. Zobacz jak jest na podanej przeze mnie stronie ( http://fsmoto.pl/pl/katalizatory ). Po kliknięciu dopiero menu się rozwija.
markonix
To kiedy i jak się rozwija to już warstwa prezentacji (albo ukrywasz podkategorie i po kliknięciu je odkrywasz, albo po kliknięciu pobierasz ajaxem).
Podany wyżej kod jest prawidłowy i uniwersalny.
sidoke
Chodzi o to, żeby pobierać AJAX'em. Nie mogę jakoś sobie z tym poradzić, mógłby ktoś pomóc? Sprawa wygląda tak: są 3 tabele: marka, model oraz wersja modelu. Na początek mają się wyświetlać tylko marki pojazdów, następnie po rozwinięciu np. marki 'audi' mają ukazać się modele (A3, A4, A5, A6... itd), a po rozwinięciu jakiegoś modelu mają się ukazać wersje tych modeli, np. dla A4 - B5, B6, B7, B8...

Pozdrawiam.
markonix
No nie ma tu zbędnej filozofii - po kliknięciu w jeden link pobierasz ajaxem modele danej marki w json albo bezpośredno w htmlu i wklejasz pod menu.
http://jsfiddle.net/willemvb/q96FK/
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.