Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][MySQL] Menu rozwijane pionowe
Forum PHP.pl > Forum > PHP
blackneron
Szukam menu kaskadowe pod którego bym podłączył baze danych.

Próbowałem kilka menusów przerwobić (m.in. Mygosu, CjgMenu) i nie udało się dla PHP i MySQL.

Znacie może jakieś menu rozwijane przystosowane pod php i mysql questionmark.gif
mike
Cytat(blackneron @ 22.08.2007, 12:18:33 ) *
Znacie może jakieś menu rozwijane przystosowane pod php i mysql questionmark.gif
Tak, Mygosu.
Najlepsze menu dostosowane do rozszerzania i podłączania w PHP.
Jeśli nie poradziłeś sobie z najprostszym menu w sieci nie wiem po co szukasz innych tongue.gif

Pokaż może jak to robiłeś. Na pewno błędnie.
Pomożemy Ci.
blackneron
Mam cztery tabele:

menu
-------
id_kat (klucz podstawowy)
nazwa_kat_pl

menu_pod
-------
id_pod (klucz podstawowy)
id_kat
nazwa_kat_pl

menu_pod_pod
-------
id_pod_pod (klucz podstawowy)
id_pod
id_kat
nazwa_kat_pl

menu_pod_pod_pod
-------
id_pod_pod_pod (klucz podstawowy)
id_pod_pod
id_kat
nazwa_kat_pl


Zrobiłem wyświetlenie kategorii, podkategorii, podpodkat:


  1. <?php
  2. $m1=mysql_query("select * from menu where id_kat>1");
  3. $m2=mysql_query("select * from menu_pod where id_pod>1");
  4. $m3=mysql_query("select * from menu_pod_pod where id_pod_pod>1");
  5. $m4=mysql_query("select * from menu_pod_pod_pod where id_pod_pod_pod>1");
  6.  
  7. print "<table cellpadding='2' cellspacing='0' border='1'>";
  8.  
  9.  
  10. while($ch1=mysql_fetch_array($m1)) 
  11. {
  12.  print "<tr><td> • ".$ch1[nazwa_kat_pl]."</li></td></tr>";
  13.  while($ch2=mysql_fetch_array($m2))
  14.  {
  15. print "<tr><td> > ".$ch2[nazwa_kat_pl]."</td></tr>";
  16.  while($ch3=mysql_fetch_array($m3))
  17.  {
  18.  print "<tr><td> >> ".$ch3[nazwa_kat_pl]."</td></tr>";
  19. while($ch4=mysql_fetch_array($m4))
  20. {
  21.  print "<tr><td> >>> ".$ch4[nazwa_kat_pl]."</td></tr>";
  22. }
  23.  }
  24.  }
  25.  
  26. }
  27. ?>




No i próbowałem przerobić ten kod:

  1. <table cellspacing="0" cellpadding="0" id="menu1" class="ddmx">
  2. <tr>
  3. <td>
  4. <a class="item1" href="javascript:void(0)">Products</a>
  5. <div class="section">
  6. <a class="item2" href="example2.html">Product One</a>
  7. <a class="item2 arrow" href="javascript:void(0)">Product Two<img src="images/arrow1.gif" width="10" height="12" alt="" /></a>
  8. <div class="section">
  9. <a class="item2" href="example2.html">Overview</a>
  10. <a class="item2" href="example2.html">Features</a>
  11. <a class="item2" href="example2.html">Requirements</a>
  12. <a class="item2" href="example2.html">Flash Demos</a>
  13. </div>
  14. <a class="item2 arrow" href="javascript:void(0)">Product Three<img src="images/arrow1.gif" width="10" height="12" alt="" /></a>
  15. <div class="section">
  16. <a class="item2" href="example2.html">Overview</a>
  17. <a class="item2" href="example2.html">Features</a>
  18. <a class="item2" href="example2.html">Requirements</a>
  19. <a class="item2" href="example2.html">Screenshots</a>
  20. <a class="item2" href="example2.html">Flash Demos</a>
  21. <a class="item2 arrow" href="javascript:void(0)">Live Demo<img src="images/arrow1.gif" width="10" height="12" alt="" /></a>
  22. <div class="section">
  23. <a class="item2" href="example2.html">Create Account</a>
  24. <a class="item2 arrow" href="javascript:void(0)">Test Drive<img src="images/arrow1.gif" width="10" height="12" alt="" /></a>
  25. <div class="section">
  26. <a class="item2" href="example2.html">Test One</a>
  27. <a class="item2" href="example2.html">Test Two</a>
  28. <a class="item2" href="example2.html">Test Three</a>
  29. </div>
  30. </div>
  31. </div>
  32. <a class="item2 arrow" href="javascript:void(0)">Product Four<img src="images/arrow1.gif" width="10" height="12" alt="" /></a>
  33. <div class="section">
  34. <a class="item2" href="example2.html">Overview</a>
  35. <a class="item2" href="example2.html">Features</a>
  36. <a class="item2" href="example2.html">Requirements</a>
  37. </div>
  38. <a class="item2" href="example2.html">Product Five</a>
  39. </div>
  40.  
  41. <a class="item1" href="javascript:void(0)">Downloads</a>
  42. <div class="section">
  43. <a class="item2" href="example2.html">30-day Demo Key</a>
  44. <a class="item2 arrow" href="javascript:void(0)">Product One Download<img src="images/arrow1.gif" width="10" height="12" alt="" /></a>
  45. <div class="section">
  46. <a class="item2" href="example2.html">Linux Download</a>
  47. <a class="item2" href="example2.html">Solaris Download</a>
  48. <a class="item2" href="example2.html">Windows Download</a>
  49. </div>
  50. <a class="item2 arrow" href="javascript:void(0)">Product Two Download<img src="images/arrow1.gif" width="10" height="12" alt="" /></a>
  51. <div class="section">
  52. <a class="item2" href="example2.html">Linux Download</a>
  53. </div>
  54. </div>
  55.  
  56. <a class="item1" href="javascript:void(0)">Support</a>
  57. <div class="section">
  58. <a class="item2" href="example2.html">E-mail Support</a>
  59. <a class="item2" href="example2.html">Phone Support</a>
  60. <a class="item2" href="example2.html">WWW support</a>
  61. </div>
  62.  
  63. <a class="item1" href="javascript:void(0)">Partners</a>
  64. <div class="section">
  65. <a class="item2" href="example2.html">Benefits</a>
  66. <a class="item2 arrow" href="javascript:void(0)">Applications<img src="images/arrow1.gif" width="10" height="12" alt="" /></a>
  67. <div class="section">
  68. <a class="item2" href="example2.html">Application One</a>
  69. <a class="item2" href="example2.html">Application Two</a>
  70. <a class="item2" href="example2.html">Application Three</a>
  71. <a class="item2" href="example2.html">Application Four</a>
  72. <a class="item2" href="example2.html">Application Five</a>
  73. <a class="item2" href="example2.html">Application Six</a>
  74. <a class="item2" href="example2.html">Application Seven</a>
  75. </div>
  76. <a class="item2" href="example2.html">Listing</a>
  77. </div>
  78.  
  79. <a class="item1" href="javascript:void(0)">Customers</a>
  80. <div class="section">
  81. <a class="item2" href="example2.html">Customer One</a>
  82. <a class="item2" href="example2.html">Customer Two</a>
  83. <a class="item2" href="example2.html">Customer Three</a>
  84. </div>
  85.  
  86. <a class="item1 bottom" href="javascript:void(0)">About Us</a>
  87. <div class="section">
  88. <a class="item2" href="example2.html">Executive Team</a>
  89. <a class="item2" href="example2.html">Investors</a>
  90. <a class="item2" href="example2.html">Career Opportunities</a>
  91. <a class="item2 arrow" href="javascript:void(0)">Press Center<img src="images/arrow1.gif" width="10" height="12" alt="" /></a>
  92. <div class="section">
  93. <a class="item2" href="example2.html">Products Information</a>
  94. </div>
  95. <a class="item2" href="example2.html">Success Stories</a>
  96. <a class="item2" href="example2.html">Contact Us</a>
  97. </div>
  98. </td>
  99. </tr>
  100. </table>



Błąd już przy drugiej pętelce, nie rozwija najeżdżając na kategorie główne:

  1. <?php
  2. print "<table cellspacing='0' cellpadding='0' id='menu1' class='ddmx'>";
  3.  while($ch1=mysql_fetch_array($m1))
  4.  {
  5. print "<tr><td><a class='item1' href='javascript:void(0)'>$ch1[nazwa_kat_pl]</a>";
  6.  }
  7.  
  8.  print "<div class='section'>";
  9.  
  10.  while($ch2=mysql_fetch_array($m2)) 
  11.  {
  12. print "<a class=item2 arrow' href='javascript:void(0)'>$ch2[nazwa_kat_pl]<img src='images/arrow1.gif' width='10' height='12' alt='' /></a>";
  13.  }
  14. ?>
klocu
ojojojoj stary zameczysz sie wprowadzajac te dane. dla mnie rozwiazanie bez sensu.

najlepsze rozwiazanie?

tabela id; nazwa; podrzedny; poziom

podrzedny to numer id rekordu do ktorgo sie odnosi.
pobierasz najwyzszy poziom (0 to trzon itd) i od konca sprawdzasz i dopisujesz do tablicy. sam stosuje takie rozwiazanie i chodzi pieknie. trzeba tylko miec pomysl i rozwiazanie przychodzi samo... bo majac np podrzednosc 50 stopnia masz 50 tabel ktore po dluzszym czasie nic Ci nie powiedza a wiec po co komplikowac sobie zycie?
blackneron
To menu robie pod mojego CMS'a. Miało być dynamiczne - w sensie elastyczne ( co do kategorii, kilku poziomów podkategorii ).
Już siedzę jakiś czas przy tym i ogólnie wychodzi z tego "kaszana".


Cytat(mike @ 22.08.2007, 12:22:45 ) *
Tak, Mygosu.
Najlepsze menu dostosowane do rozszerzania i podłączania w PHP.
Jeśli nie poradziłeś sobie z najprostszym menu w sieci nie wiem po co szukasz innych tongue.gif

Pokaż może jak to robiłeś. Na pewno błędnie.
Pomożemy Ci.


Jak taki chojrak jesteś mike to czekam na Twoje sugestie. Jak Ty byś to zrobił ?
mike
Zanim zabierzesz się za menu musisz zająć się bazą i przygotowaniem danych, które to menu tworzą.
Rozwiązanie, które masz z tymi tabelami jest niedorzeczne.
Wszystko jesteś w stanie zamknąć w jednej tabeli:

menu
-------
id (PK)
parent_id (FK) (klucz obcy wskazujący na klucz główny z tej samej tabeli)
name

Idź w tym kierunku.
Jak zrobisz to po ludzku to pogadamy dalej tongue.gif
phpion
Może przykładowe dane dla tabeli przedstawionej przez mike'go bardziej zobrazują Ci sytuację:
Kod
id : parent_id : name
1 : NULL : Kategoria_1
2 : NULL : Kategoria_2
3 : 1 : Kategoria_1_1
4 : 3 : Kategoria_1_1_1
5 : 1 : Kategoria_1_2

Na takich danych jesteś w stanie zbudować odpowiednie drzewko.

PS: Twoja struktura z 4 tabelami pozwala stworzyć strukturę maksymalnie na 4 kategorie wgłąb, natomiast w powyższym przykładzie nie masz ograniczenia co do rozpiętości drzewa.
blackneron
Zrobiłem po swojemu i działa. guitar.gif
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.