Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Dzewka - ostatnie liscie
Forum PHP.pl > Forum > Przedszkole
ganimedes
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?
orson
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
ganimedes
ajjjj, mój angielski leży sad.gif((((((((((
orson
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
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.