Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z funkcja
Forum PHP.pl > Forum > PHP
Vomit
W manualu znalazłem funcje ktora tworzy liste katalogow i podkatalogow wraz z plikami:

  1. <?php
  2. function getFiles( & $rdi, $depth = 0)
  3. {
  4. $txt = '';
  5. if (!is_object($rdi))
  6. return;
  7.  
  8. for ($rdi->rewind();$rdi->valid();$rdi->next())
  9. {
  10. if ($rdi->isDot())
  11. continue;
  12.  
  13. if ($rdi->isDir() || $rdi->isFile())
  14. {
  15.  
  16. for ($i = 0; $i<=$depth;++$i)
  17. echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  18.  
  19. if ( $rdi->isDir() )
  20. {
  21. echo '<b>' . $rdi->current().'</b><br />';
  22. }
  23. elseif ( $rdi->isFile() )
  24. {
  25. echo '<a href="' . $_SERVER['PHP_SELF'] . '?action=' . $rdi->key() . '">' . $rdi->current().'</a><br />';
  26. }
  27.  
  28. if ($rdi->hasChildren())
  29. getFiles($rdi->getChildren(),1+$depth);
  30. }
  31. }
  32. }
  33. getFiles(new RecursiveDirectoryIterator('code/'));
  34.  
  35. ?>


Chcialem troche ja zmienic, abym mogl jej uzywac razem z szablonami:

  1. <?php
  2. function getFiles( & $rdi, $depth = 0)
  3. {
  4. $txt = '';
  5. if (!is_object($rdi))
  6. return;
  7.  
  8. for ($rdi->rewind();$rdi->valid();$rdi->next())
  9. {
  10. if ($rdi->isDot())
  11. continue;
  12.  
  13. if ($rdi->isDir() || $rdi->isFile())
  14. {
  15.  
  16. for ($i = 0; $i<=$depth;++$i)
  17. $txt .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  18.  
  19. if ( $rdi->isDir() )
  20. {
  21. $txt .= '<b>' . $rdi->current().'</b><br />';
  22. }
  23. elseif ( $rdi->isFile() )
  24. {
  25. $txt .= '<a href="' . $_SERVER['PHP_SELF'] . '?action=' . $rdi->key() . '">' . $rdi->current().'</a><br />';
  26. }
  27.  
  28. if ($rdi->hasChildren())
  29. getFiles($rdi->getChildren(),1+$depth);
  30. }
  31. }
  32. return $txt;
  33. }
  34.  
  35. $pic = getFiles(new RecursiveDirectoryIterator('code/'));
  36. echo $pic;
  37.  
  38. ?>


Niestety nie jest zwracane przez nia to samo co pierwsza wersja. Czemu ? :|
stoprocent
Poniewaz zapetlasz funkcje i do zmienneij txt dodajesz wartosci ale za kazdym wywolaniem funkcji czyscisz zminna txt
  1. <?php
  2. function getFiles( & $rdi, $depth = 0)
  3. {
  4. $txt = ''; // A tu czyscisz zmienna 
  5. if (!is_object($rdi))
  6. return;
  7.  
  8. for ($rdi->rewind();$rdi->valid();$rdi->next())
  9. {
  10. if ($rdi->isDot())
  11. continue;
  12.  
  13. if ($rdi->isDir() || $rdi->isFile())
  14. {
  15.  
  16. for ($i = 0; $i<=$depth;++$i)
  17. $txt .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  18.  
  19. if ( $rdi->isDir() )
  20. {
  21. $txt .= '<b>' . $rdi->current().'</b><br />';
  22. }
  23. elseif ( $rdi->isFile() )
  24. {
  25. $txt .= '<a href="' . $_SERVER['PHP_SELF'] . '?action=' . $rdi->key() . '">' . $rdi->current().'</a><br />';
  26. }
  27.  
  28. if ($rdi->hasChildren())
  29. getFiles($rdi->getChildren(),1+$depth); //Tutaj wywolujesz funkcje getFiles
  30. }
  31. }
  32. return $txt;
  33. }
  34.  
  35. $pic = getFiles(new RecursiveDirectoryIterator('code/'));
  36. echo $pic;
  37.  
  38. ?>


polecam to zrobic tak

  1. <?php
  2. function getFiles( & $rdi, $depth = 0, $txt = '') // Zmiana 1
  3. {
  4.  
  5. if (!is_object($rdi))
  6. return;
  7.  
  8. for ($rdi->rewind();$rdi->valid();$rdi->next())
  9. {
  10. if ($rdi->isDot())
  11. continue;
  12.  
  13. if ($rdi->isDir() || $rdi->isFile())
  14. {
  15.  
  16. for ($i = 0; $i<=$depth;++$i)
  17. $txt .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  18.  
  19. if ( $rdi->isDir() )
  20. {
  21. $txt .= '<b>' . $rdi->current().'</b><br />';
  22. }
  23. elseif ( $rdi->isFile() )
  24. {
  25. $txt .= '<a href="' . $_SERVER['PHP_SELF'] . '?action=' . $rdi->key() . '">' . $rdi->current().'</a><br />';
  26. }
  27.  
  28. if ($rdi->hasChildren())
  29. getFiles($rdi->getChildren(),1+$depth, $txt); // Zmiana 2
  30. }
  31. }
  32. return $txt;
  33. }
  34.  
  35. $pic = getFiles(new RecursiveDirectoryIterator('code/'));
  36. echo $pic;
  37.  
  38. ?>
Vomit
Tak tez nie zadziała. Ale juz doszedłem do tego. Poszukiwane przeze mnie rozwiazanie to:

  1. <?php
  2. function getFiles( & $rdi, $depth = 0, $txt = '') // Zmiana 1
  3. {
  4.  
  5. if (!is_object($rdi))
  6. return;
  7.  
  8. for ($rdi->rewind();$rdi->valid();$rdi->next())
  9. {
  10. if ($rdi->isDot())
  11. continue;
  12.  
  13. if ($rdi->isDir() || $rdi->isFile())
  14. {
  15.  
  16. for ($i = 0; $i<=$depth;++$i)
  17. $txt .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  18.  
  19. if ( $rdi->isDir() )
  20. {
  21. $txt .= '<b>' . $rdi->current().'</b><br />';
  22. }
  23. elseif ( $rdi->isFile() )
  24. {
  25. $txt .= '<a href="' . $_SERVER['PHP_SELF'] . '?action=' . $rdi->key() . '">' . $rdi->current().'</a><br />';
  26. }
  27.  
  28. if ($rdi->hasChildren())
  29. txt .= getFiles($rdi->getChildren(),1+$depth, $txt); // Zmiana 2
  30. }
  31. }
  32. return $txt;
  33. }
  34.  
  35. $pic = getFiles(new RecursiveDirectoryIterator('code/'));
  36. echo $pic;
  37.  
  38. ?>
stoprocent
Cytat
Tak tez nie zadziała. Ale juz doszedłem do tego. Poszukiwane przeze mnie rozwiazanie to:


Tak z ciekawosci bo nierozumiem , napisales ze tak nie zadziala i wkleiles moje porpawki jakos poszukiwane rozwiazanie blink.gif
Vomit
Twoja linijka:
  1. <?php
  2. getFiles($rdi->getChildren(),1+$depth, $txt); 
  3. ?>


Moja:
  1. <?php
  2. txt .= getFiles($rdi->getChildren(),1+$depth, $txt); 
  3. ?>


Rozni sie tylko tym. Ale roznica jest naprawde spora.
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.