Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> zgłupiałam - co jest nie tak?
chrupka
post 14.09.2007, 14:54:08
Post #1





Grupa: Zarejestrowani
Postów: 28
Pomógł: 0
Dołączył: 11.01.2007

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


Witam, w swoim skrypcie użyłam funkcji znalezionej na tym forum do rysowania drzewa kategorii:

function set_tree($nodeLevel=1, $nodeParent=0)
{
$sql="SELECT * FROM offers WHERE parent = $nodeParent";
$nodeElements = mysql_query($sql);

$margin = $nodeLevel * 10;
while( $nodeEl=mysql_fetch_assoc($nodeElements)) {
echo "<tr><td style=\"padding-left:".$margin."px;\">".$nodeEl['name']."</td></tr>";
//$m[]="<tr><td style=\"padding-left:".$margin."px;\">".$nodeEl['name']."</td></tr>";
set_tree($nodeLevel+1, $nodeEl['id']);
}
//return $m;
}

I nie wiem czemu gdy odkomentuje funkcję, tak aby zwracała tablicę (lub po prostu string $m.="<tr><td style.....") to zawiera ona co innego niż to co wypisuje funkcja echem.
Dla przykładu, w tabeli offers mam 4 rekordy:
--Lista ofertowa
----Eksport leków gotowych
----Eksport substancji farmaceutycznych
--Pozostałe leki

'Echo' wypluwa wszystko prawidłowo i po kolei przy wywołaniu funkcji: $moo=set_tree(); natomiast var_dump($moo) (czy też echo $moo przy zamianie tablicy na stringa) wypluwa tylko "Listę ofertową" i "Pozostałe leki".
o co chodzi?:[
Go to the top of the page
+Quote Post
NoiseMc
post 14.09.2007, 15:03:39
Post #2





Grupa: Zarejestrowani
Postów: 398
Pomógł: 10
Dołączył: 24.11.2004
Skąd: Łódź

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


Dlatego ze przy kazdym kolejnym rekurencyjnym wywolaniu set_tree() $m jest puste, albo zrob $m globalna zmienna, albo sprobuj tak (nie testowalem)
  1. <?php
  2. function get_tree ($nodeLevel = 1, $nodeParent = 0)
  3. {
  4. $sql = "SELECT * FROM offers WHERE parent = $nodeParent";
  5. $nodeElements = mysql_query ($sql);
  6.  
  7. $margin = $nodeLevel * 10;
  8.  
  9. while ($nodeEl = mysql_fetch_assoc ($nodeElements))
  10. {
  11. $m[] = "<tr><td style=\"padding-left:".$margin."px;\">" . $nodeEl['name'] . "</td></tr>";
  12.  
  13. $m = array_merge ($m, get_tree ($nodeLevel + 1, $nodeEl['id']));
  14. }
  15.  
  16. return $m;
  17. }
  18. ?>
  19.  
  20. print_r (get_tree ());


Zmienilem nazwe funkcji na get_tree, bo zwraca drzewo a nie je ustawia smile.gif

Ten post edytował NoiseMc 14.09.2007, 15:06:34


--------------------
Go to the top of the page
+Quote Post
chrupka
post 14.09.2007, 15:12:34
Post #3





Grupa: Zarejestrowani
Postów: 28
Pomógł: 0
Dołączył: 11.01.2007

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


wrzuciłam globalną i gra guitar.gif
przy array_merge wywaliło mi, że drugi argument nie jest tablicą (?) - ale nie mam czasu tego rozgryzać
dzięki i pozdrawiam :-))
Go to the top of the page
+Quote Post
NoiseMc
post 14.09.2007, 15:28:37
Post #4





Grupa: Zarejestrowani
Postów: 398
Pomógł: 10
Dołączył: 24.11.2004
Skąd: Łódź

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


Jezeli get_tree nie zwroci nic to faktycznie nie bedzie tablica, moznaby to jeszcze poprawic:
  1. <?php
  2. function get_tree ($nodeLevel = 1, $nodeParent = 0)
  3. {
  4. $sql = "SELECT * FROM offers WHERE parent = $nodeParent";
  5. $nodeElements = mysql_query ($sql);
  6.  
  7. $margin = $nodeLevel * 10;
  8.  
  9. while ($nodeEl = mysql_fetch_assoc ($nodeElements))
  10. {
  11. $m[] = "<tr><td style=\"padding-left:".$margin."px;\">" . $nodeEl['name'] . "</td></tr>";
  12.  
  13. $subNodes = get_tree ($nodeLevel + 1, $nodeEl['id']);
  14.  
  15. if (! empty ($subNodes))
  16. {
  17. $m = array_merge ($m, $subNodes);
  18. }
  19. }
  20.  
  21. return $m;
  22. }
  23. ?>


Ten post edytował NoiseMc 14.09.2007, 15:29:41


--------------------
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: 19.07.2025 - 19:46