Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Przeróbka skryptu
Forum PHP.pl > Forum > Przedszkole
Słoń Edward
Hej! Znalazłem gdzieś w przykładach taki skrypcik. Chciałbym by ten oto skrypcik pokazywał pliki w odwrotnej kolejności. Wiem, że trzeba coś zrobić z tablicą, ale nie za bardzo się na tym znam...
Teraz, gdy mam pliki x2.php, x11.php, x5.php, x24.php, x9.php, x13.php, x10.php, wyświetla mi je w kolejności:

-x.php
-x.php
-x.php
-x.php
-x.php
-x.php
-x.php

A ja bym chciał, by wyświetlił je w takiej:

-x.php
-x.php
-x.php
-x.php
-x.php
-x.php
-x.php

Aoto kod:

  1. <?php
  2.  
  3.  # Do we have a path? if not, it's the current directory
  4.  $path = $_GET[&#092;"path\"];
  5.  if( !isset( $path ) || $path == &#092;"\" ) {
  6.  $path = &#092;".\";
  7.  }
  8.  
  9.  //# Print out for navigation
  10.  //print \"Current path: <b>\" . $path . \"</b><br />\";
  11.  
  12.  # Initialise list arrays, directories and files separately and array counters fo
  13.  them
  14.  $d_arr = array(); $d = 0;
  15.  $f_arr = array(); $f = 0;
  16.  
  17.  # Open possibly available directory
  18.  if( is_dir( $path ) ) {
  19.  if( $handle = opendir( $path ) ) {
  20.  while( false !== ( $file = readdir( $handle ) ) ) {
  21.  # Make sure we don't push parental directories or dotfiles (unix) into the arrays
  22.  if( $file != &#092;".\" && $file != \"..\" && $file[0] != \".\" ) {
  23.  if( is_dir( $path . &#092;"/\" . $file ) )
  24.  # Create array for directories
  25.  $d_arr[$d++] = $file;
  26.  else
  27.  # Create array for files
  28.  $f_arr[$f++] = $file;
  29.  }
  30.  }
  31.  }
  32.  }
  33.  
  34.  # Wrap things up if we're in a directory
  35.  if( is_dir( $handle ) ) closedir( $handle );
  36.  
  37.  # Sort and reset the arrays
  38.  asort( $d_arr ); reset( $d_arr );
  39.  asort( $f_arr ); reset( $f_arr );
  40.  
  41.  # Print a parent directory link
  42.  $d_prev = substr( $path, 0, ( strrpos( dirname( $path . &#092;"/.\" ), \"/\" ) ) );
  43.  print &#092;"<a href=\"?path=\" . $d_prev . \"\"> Spis kategorii </a><br />n\";
  44.  
  45.  # Print the directory list
  46.  for( $i=0; $i < count( $d_arr ); $i++ ) {
  47.  # Print with query string
  48.  print &#092;"<img border=/\"0/\" src=/\"../folder.gif/\" width=/\"16/\" height=/\"14/\"> <a href=\"?path=\" . $path . \"/\" . $d_arr[$i] . \"\">\" . $d_arr[$i] . \"</a>/<br />n\";
  49.  }
  50.  
  51.  # Print file list
  52.  for( $i=0; $i < count( $f_arr ); $i++ ) {
  53.  $nazwa = substr($f_arr[$i], 0, strpos($f_arr[$i], '.'));
  54.  # Only print path and filename
  55.  print &#092;"<img border=/\"0/\" src=/\"../img.gif/\" width=/\"14/\" height=/\"16/\"> <a href=\"\" . $path . \"/\" . $f_arr[$i] . \"\"> \" . $f_arr[$i] . \"</a>\";
  56.  # We may want a file size. NOTE: needs $path to stat
  57.  if( filesize( $path . &#092;"/\" . $f_arr[$i] ) >= 1024 ) {
  58.  # Size in kilobytes
  59.  print &#092;" \" . round( filesize( $path . \"/\" . $f_arr[$i] ) / 1024, 1 ) . \" KB<br />n\";
  60.  } elseif( filesize( $path . &#092;"/\" . $f_arr[$i] ) >= 1048576 ) {
  61.  # Size in megabytes
  62.  print &#092;" \" . round( filesize( $path . \"/\" . $f_arr[$i] ) / 1024 / 1024, 1 ) . \" MB<br />n\";
  63.  } else {
  64.  # Size in bytes
  65.  print &#092;" \" . filesize( $path . \"/\" . $f_arr[$i] ) . \" bytes<br />n\";
  66.  }
  67.  }
  68.  
  69. ?>


---

aleksander
dr_bonzo
Daj array_reverse() przed wyswietlaniem danych lub petle wykonuj od konca.
Uzywaj bbcodu [ php ] dla kodu php.
James
Szczerze mówiąc to przynajmniej mi trudno jest się skapować o jaką kolejnośc Ci chodzi...bo dla mnie obie listy plików pokazane przez Ciebie są identyczne, no chyba, że z moim wzrokiem jest coś nie tak...
Radarek
Cytat(James @ 2005-06-13 16:32:06)
Szczerze mówiąc to przynajmniej mi trudno jest się skapować o jaką kolejnośc Ci chodzi...bo dla mnie obie listy plików pokazane przez Ciebie są identyczne, no chyba, że z moim wzrokiem jest coś nie tak...

Nie tylko ty smile.gif. Zapewne autor postu wpierw wkleil sobie cala kolumne '-x.php' z mysla ze wstawi odpowiedni numerki pozniej... i zapomnial o tym smile.gif.
Słoń Edward
No tak sorry za brak numerków... smile.gif
Ale dzięki, że się ktoś skapował.

Normalna lista

x2.php,
x5.php,
x9.php,
x10.php
x11.php,
x13.php,
x24.php,

Lista poszukiwana smile.gif

x24.php,
x13.php,
x11.php,
x10.php
x9.php,
x5.php,
x2.php,
dr_bonzo
Mowilem:
array_reverse( $f_arr ), przed wyswietleniem.
Guest
Czyli to ma wyglądać tak? Jeśli tak to nie działa...

Dodałem array_reverse do lini 55

  1. <?php
  2.  
  3.  # Do we have a path? if not, it's the current directory
  4.  $path = $_GET[&#092;"path\"];
  5.  if( !isset( $path ) || $path == &#092;"\" ) {
  6.  $path = &#092;".\";
  7.  }
  8.  
  9.  //# Print out for navigation
  10.  //print \"Current path: <b>\" . $path . \"</b><br />\";
  11.  
  12.  # Initialise list arrays, directories and files separately and array counters fo
  13.  them
  14.  $d_arr = array(); $d = 0;
  15.  $f_arr = array(); $f = 0;
  16.  
  17.  # Open possibly available directory
  18.  if( is_dir( $path ) ) {
  19.  if( $handle = opendir( $path ) ) {
  20.  while( false !== ( $file = readdir( $handle ) ) ) {
  21.  # Make sure we don't push parental directories or dotfiles (unix) into the arrays
  22.  if( $file != &#092;".\" && $file != \"..\" && $file[0] != \".\" ) {
  23.  if( is_dir( $path . &#092;"/\" . $file ) )
  24.  # Create array for directories
  25.  $d_arr[$d++] = $file;
  26.  else
  27.  # Create array for files
  28.  $f_arr[$f++] = $file;
  29.  }
  30.  }
  31.  }
  32.  }
  33.  
  34.  # Wrap things up if we're in a directory
  35.  if( is_dir( $handle ) ) closedir( $handle );
  36.  
  37.  # Sort and reset the arrays
  38.  asort( $d_arr ); reset( $d_arr );
  39.  asort( $f_arr ); reset( $f_arr );
  40.  
  41.  # Print a parent directory link
  42.  $d_prev = substr( $path, 0, ( strrpos( dirname( $path . &#092;"/.\" ), \"/\" ) ) );
  43.  print &#092;"<a href=\"?path=\" . $d_prev . \"\"> Spis kategorii </a><br />n\";
  44.  
  45.  # Print the directory list
  46.  for( $i=0; $i < count( $d_arr ); $i++ ) {
  47.  # Print with query string
  48.  print &#092;"<img border=/\"0/\" src=/\"../folder.gif/\" width=/\"16/\" height=/\"14/\"> <a href=\"?path=\" . $path . \"/\" . $d_arr[$i] . \"\">\" . $d_arr[$i] . \"</a>/<br />n\";
  49.  }
  50.  
  51.  # Print file list
  52.  array_reverse( $f_arr );
  53.  for( $i=0; $i < count( $f_arr ); $i++ ) {
  54.  $nazwa = substr($f_arr[$i], 0, strpos($f_arr[$i], '.'));
  55.  # Only print path and filename
  56.  print &#092;"<img border=/\"0/\" src=/\"../img.gif/\" width=/\"14/\" height=/\"16/\"> <a href=\"\" . $path . \"/\" . $f_arr[$i] . \"\"> \" . $f_arr[$i] . \"</a>\";
  57.  # We may want a file size. NOTE: needs $path to stat
  58.  if( filesize( $path . &#092;"/\" . $f_arr[$i] ) >= 1024 ) {
  59.  # Size in kilobytes
  60.  print &#092;" \" . round( filesize( $path . \"/\" . $f_arr[$i] ) / 1024, 1 ) . \" KB<br />n\";
  61.  } elseif( filesize( $path . &#092;"/\" . $f_arr[$i] ) >= 1048576 ) {
  62.  # Size in megabytes
  63.  print &#092;" \" . round( filesize( $path . \"/\" . $f_arr[$i] ) / 1024 / 1024, 1 ) . \" MB<br />n\";
  64.  } else {
  65.  # Size in bytes
  66.  print &#092;" \" . filesize( $path . \"/\" . $f_arr[$i] ) . \" bytes<br />n\";
  67.  }
  68.  }
  69.  
  70. ?>

[SIZE=1][color=green]
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.