Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP] Menu wielopoziomowe
marek71_33
post 25.07.2019, 13:07:15
Post #1





Grupa: Zarejestrowani
Postów: 38
Pomógł: 0
Dołączył: 2.02.2010

Ostrzeżenie: (0%)
-----


Chcę utworzyć memu wielopoziomowe. Mam tabelę
  1. page_id | page_parent | page_subject
  2. 9 | NULL | Kategoria 1
  3. 10 | 9 | Podkategoria 1.1
  4. 11| 9 | Podkategoria 1.2
  5. 12| 10 | Podkategoria 1.1.1
  6. 13| 10 | Podkategoria 1.1.2
  7. 14| 12 | Podkategoria 1.1.1.1
  8. 16| NULL | Kategoria 2
  9. 18 | NULL | Kategoria 3


Chciałbym uzyskać takie coś:
  1. Kategoria 1
  2. Podkategoria 1.1
  3. Podkategoria 1.1.1
  4. Podkategoria 1.1.1.1
  5. Podkategoria 1.1.2
  6. Podkategoria 1.2
  7. Kategoria 2
  8. Kategoria 3


Natomiast otrzymuję takie coś:
  1. Kategoria 1
  2. Podkategoria 1.1
  3. Podkategoria 1.1.1
  4. Podkategoria 1.1.1.1
  5. Podkategoria 1.1.2
  6. Podkategoria 1.2


Mój kod:
  1. $pdo = new PDO("mysql:dbname=07117910_sp5;host=localhost","root","");
  2.  
  3. $query = $pdo->query("SELECT * FROM page_v ORDER BY page_order, page_matrix");
  4.  
  5. $family_tree = [];
  6. $root_parent = -1;
  7. $root_parent_name = "";
  8.  
  9. function makeTree($query,&$family_tree,&$root_parent,&$root_parent_name){
  10. while($row = $query->fetch(PDO::FETCH_ASSOC)){
  11. if(is_null($row['page_parent'])){
  12. $root_parent = $row['page_id'];
  13. $root_parent_name = $row['page_subject'];
  14. }else{
  15. if(!isset($family_tree[$row['page_parent']])){
  16. $family_tree[$row['page_parent']] = [];
  17. }
  18. $family_tree[$row['page_parent']][] = array($row['page_subject'],$row['page_id']);
  19. }
  20. }
  21. }
  22.  
  23. function buildList($family_tree,$parent){
  24. $list = "<ul>";
  25.  
  26. foreach($family_tree[$parent] as $each_child){
  27. $list .= "<li>" . $each_child[0];
  28. if(isset($family_tree[$each_child[1]])){
  29. $list .= buildList($family_tree,$each_child[1]);
  30. }
  31. $list .= "</li>";
  32. }
  33.  
  34. $list .= "</ul>";
  35.  
  36. return $list;
  37.  
  38. }
  39. makeTree($query,$family_tree,$root_parent,$root_parent_name);
  40.  
  41.  
  42. echo "<ul>";
  43. echo "<li>$root_parent_name";
  44. echo buildList($family_tree,$root_parent);
  45. echo "</li>";
  46. echo "</ul>";


Co jest nie tak z tym kodem?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
nospor
post 25.07.2019, 13:37:53
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




W sensie ze zjada ci kateria 2 kategoria 3?
No tutaj
if(is_null($row['page_parent'])){
$root_parent = $row['page_id'];
$root_parent_name = $row['page_subject'];
zalozyles ze masz tylko jeden root wiec masz co masz. Przeciez masz 3 root. Musisz to wlasnie poprawic


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
marek71_33
post 25.07.2019, 13:53:50
Post #3





Grupa: Zarejestrowani
Postów: 38
Pomógł: 0
Dołączył: 2.02.2010

Ostrzeżenie: (0%)
-----


Wywaliłem ten warunek

  1. if(is_null($row['page_parent'])


ale nie pomogło, nie kumam tego blędu
Go to the top of the page
+Quote Post
nospor
post 25.07.2019, 14:04:04
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Ja nie mowielm o warunki isNULL tylko o tresci blocku tego IF

$root_parent = $row['page_id'];
$root_parent_name = $row['page_subject'];

O tutaj zakladasz ze masz tylko jeden root bo ciagle nadpisujesz jedna zmienna. Nie zmieniajac za bardzo twojego kodu, powinienies wszystkie root zapisywac do tablicy, a potem przerobic ten kod


echo "<ul>";
echo "<li>$root_parent_name";
echo buildList($family_tree,$root_parent);
echo "</li>";
echo "</ul>";

by lecial po tej tablicy ROOTow a nie tylko po tej jednej zmiennej z jednym root


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 13.08.2025 - 23:25