Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Dzewka - ostatnie liscie
ganimedes
post 24.07.2005, 10:48:03
Post #1





Grupa: Zarejestrowani
Postów: 66
Pomógł: 0
Dołączył: 1.04.2004

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


Witam stoje przed problemem napisania skryptu ktory wyświetlałby "ostatnie liście" w drzewie kategorii. Kożystam z rozwiązania ktore jest na sitepoint http://www.sitepoint.com/article/hierarchical-data-database/
  1. <?php 
  2. function display_tree($root) { 
  3.  // retrieve the left and right value of the $root node 
  4.  $result = mysql_query('SELECT lft, rgt FROM tree '. 
  5. 'WHERE title=\"'.$root.'\";'); 
  6.  $row = mysql_fetch_array($result); 
  7.  
  8.  // start with an empty $right stack 
  9.  $right = array(); 
  10.  
  11.  // now, retrieve all descendants of the $root node 
  12.  $result = mysql_query('SELECT title, lft, rgt FROM tree '. 
  13. 'WHERE lft BETWEEN '.$row['lft'].' AND '. 
  14. $row['rgt'].' ORDER BY lft ASC;'); 
  15.  
  16.  // display each row 
  17.  while ($row = mysql_fetch_array($result)) { 
  18.  // only check stack if there is one 
  19.  if (count($right)>0) { 
  20.  // check if we should remove a node from the stack 
  21.  while ($right[count($right)-1]<$row['rgt']) { 
  22.  array_pop($right); 
  23.  } 
  24.  } 
  25.  
  26.  // display indented node title 
  27.  echo str_repeat(' ',count($right)).$row['title'].&#092;"n\"; 
  28.  
  29.  // add this node to the stack 
  30.  $right[] = $row['rgt']; 
  31.  } 
  32. } 
  33. ?>

Podaje ID rodzica np Food i chce zeby wyświetliło mi ostatnie liście czyli Cheery, Banana, Beef, Pork

Siedze przy tym juz pare godzin i nie moge wpaść na pomysł, kto może ma?
Go to the top of the page
+Quote Post
orson
post 24.07.2005, 17:45:05
Post #2





Grupa: Zarejestrowani
Postów: 548
Pomógł: 2
Dołączył: 19.07.2003

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


witam ...

może to:
Cytat
If you give me the left and right values of a node, I can tell you how many descendants it has by using a little math.

As each descendant increments the right value of the node with 2, the number of descendants can be calculated with:

descendants = (right – left - 1) / 2

With this simple formula, I can tell you that the 2-11 ‘Fruit’ node has 4 descendant nodes and that the 8-9 ‘Banana’ node is just a child, not a parent.


ostatni akapit 2 strony

pozdrawiam


--------------------
Computer games don't affect kids; I mean if <span style="font-weight: bold;">Pac Man</span> affected us as kids,we would all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music.
Go to the top of the page
+Quote Post
ganimedes
post 25.07.2005, 11:40:20
Post #3





Grupa: Zarejestrowani
Postów: 66
Pomógł: 0
Dołączył: 1.04.2004

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


ajjjj, mój angielski leży sad.gif((((((((((
Go to the top of the page
+Quote Post
orson
post 25.07.2005, 12:43:52
Post #4





Grupa: Zarejestrowani
Postów: 548
Pomógł: 2
Dołączył: 19.07.2003

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


witam ...
Cytat
descendants = (right – left - 1) / 2


Cytat
liczba potomków = (prawa liczba - lewa liczba - 1) / 2


więc jeżeli większa od 0 to znaczy że ma dzieci ... umieść warunki w select z where liczba potonków = 0 i pobierze ci wszystkie ostatnie liście

pozdrawiam


--------------------
Computer games don't affect kids; I mean if <span style="font-weight: bold;">Pac Man</span> affected us as kids,we would all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music.
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: 7.07.2025 - 00:29