OVH Cloud OVH Cloud

faire le tri sur un tableau

1 réponse
Avatar
Sebounet
Bonjour, le but du jeu est de lire les dossiers et fichiers d'un
répertoire, de les afficher dans un tableaux, et de pouvoir faire un
tri sur la colonne désirée.
Cependant, vu que c'est un tableau de tableau, j'ai des soucis pour
faire le tri... Si quelqu'un peu m'aider....

Many thanks

Seb.




<HTML>
<HEAD>
</HEAD>
<BODY>
<?PHP

$chemin = getcwd(); // récupération du nom du répertoire courant
$chemin2 = explode ("\intrahfm", $chemin);
$chemin_racine = "$chemin2[0]\intrahfm\\";
$chemin = strtr($chemin."/","\\","/"); //remplacement des caractères
'\' par '/'

//$GLOBALS[$compteurDossier];
//$GLOBALS[$compteurFichier];
//$GLOBALS[$tailleFichiers];
//$GLOBALS[$curr];
//$GLOBALS[$refTelecharger];
//$GLOBALS[$repImages];
//$GLOBALS[$chemin_racine];
//$GLOBALS[$imgDossier];
//$GLOBALS[$imgParent];

if ($curr == "") $curr = $chemin;
$compteurFichierds = opendir($curr); // ouverture du répertoire
courant
rewinddir($compteurFichierds); // Se positionne sur le
premier fichier du répertoire

$n = 0;
$d = 0;
$r = 0;

// Parametrage
$repImages = "<IMG SRC=/images/icones/";
$imgDossier = $repImages."dossier.gif BORDER=0>";
$imgParent = $repImages."parent.gif BORDER=0>";

$refTelecharger="<A HREF=telecharger.php?NomFichier=";

$imgFlecheBas="<IMG SRC=/images/icones/fleche0.gif>";
if ($tri == "n") $imgn = $imgFlecheBas;
if ($tri == "t") $imgt = $imgFlecheBas;
if ($tri == "s") $imgs = $imgFlecheBas;
if ($tri == "d") $imgd = $imgFlecheBas;


//$compteurDossier $GLOBALS;

$compteurDossier=0;
$compteurFichier=0;

$tailleFichiers=0;
function scanDir($dir){
global $compteurDossier;
$repCourant=$dir;
$repCourant = strtr($repCourant."/","\\","/");
$fds = opendir($repCourant);
rewinddir($fds);

while ($nomCourant = readdir($fds)){
//if ($nomCourant != "." && $nomCourant != "..") {
if (is_dir($repCourant.$nomCourant)){
$tabn[$compteurDossier] = $nomCourant;
$tabt[$compteurDossier] = "Dossier";
$tabs[$compteurDossier] = "";
$tabd[$compteurDossier] = date("d/m/Y
h:i:s",filemtime($nomCourant));

$tabDossier[$compteurDossier][0]=$tabn[$compteurDossier];
$tabDossier[$compteurDossier][1]=$tabt[$compteurDossier];
$tabDossier[$compteurDossier][2]=$tabs[$compteurDossier];
$tabDossier[$compteurDossier][3]=$tabd[$compteurDossier];
$compteurDossier++;
}
//}
}
return $tabDossier;
}

function scanFic($dir){
global $compteurFichier;
global $tailleFichiers;
$repCourant=$dir;
$repCourant = strtr($repCourant."/","\\","/");
$fds = opendir($repCourant);
rewinddir($fds);
while ($nomCourant = readdir($fds)){
if (is_file($repCourant.$nomCourant)){
$tabn[$compteurFichier] = $nomCourant;
$tabt[$compteurFichier] =
strtoupper(substr($nomCourant,strrpos($nomCourant,".")+1,strlen($nomCourant)-strrpos($nomCourant,".")-1));
$tabs[$compteurFichier] =
number_format((filesize($nomCourant)/1000), 3, ","," ");
$tabd[$compteurFichier] = date("d/m/Y
h:i:s",filemtime($nomCourant));

$tabFichier[$compteurFichier][0]=$tabn[$compteurFichier];
$tabFichier[$compteurFichier][1]=$tabt[$compteurFichier];
$tabFichier[$compteurFichier][2]=$tabs[$compteurFichier];
$tabFichier[$compteurFichier][3]=$tabd[$compteurFichier];
$tailleFichiers+=$tabs[$compteurFichier];
$compteurFichier++;
}
}
return $tabFichier;
}

function ecrireFichier(){
global $curr;
global $refTelecharger;
global $repImages;
global $chemin_racine;

$retourFichier = scanFic($curr);
for ($k=0; $k<count($retourFichier); $k++){
for ($l=0; $l<count($retourFichier[$k]); $l++){
$nom=$retourFichier[$k][0];
//uasort($nom, "strnatcasecmp");
$type=$retourFichier[$k][1];
$typeOrig=$retourFichier[$k][1];
$taille=$retourFichier[$k][2];
$dateModif=$retourFichier[$k][3];
}
uasort($retourFichier[$k], "strnatcasecmp");
if (!file_exists($chemin_racine."/images/icones/".$type.".gif"))
$type="defaut";
echo ("<TR><TD>".$refTelecharger.$nom.">".$repImages.$type.".gif
BORDER=0>".$nom."</a></TD><TD WIDTH=75
ALIGN=CENTER>".$typeOrig."</TD><TD ALIGN=RIGHT>".$taille." Ko</TD><TD
WIDTH=200 ALIGN=RIGHT>".$dateModif."</TD></TR>\n"); //<td><img
src=".$nom."></td>
}
}

function ecrireDossier(){
global $imgDossier;
global $curr;
global $imgParent;
$retourDossier = scanDir($curr);
for ($i=0; $i<count($retourDossier); $i++){
for ($j=0; $j<count($retourDossier[$i]); $j++){
//uasort($retourDossier[$i],"strnatcasecmp");
// while (list($k,$v) = @each($retourDossier[$i][$j])){
// $nom=$retourDossier[$i][$k];
// $type=$retourDossier[$i][$v];
$nom=$retourDossier[$i][0];
$type=$retourDossier[$i][1];
$taille="";
$DateModif=$retourDossier[$i][3];
}
//if ($nom!="."){
if ($nom=="." ){
echo ("<TR><TD COLSPAN=4><A
HREF=".$PHP_SELF."?curr=".$home.">...</TD>\n");
echo ("<TR><TD COLSPAN=4><A
HREF=".$PHP_SELF."?curr=".$curr.$nom.">".$imgParent."</TD>\n");
}elseif ($nom!="." && $nom!=".."){
//echo ("<TR><TD><A
HREF=".$PHP_SELF."?curr=".$curr.$nom."/>".$imgDossier."
".$nom."</A></TD><TD WIDTH=75 ALIGN=CENTER>".$type."</TD><TD
ALIGN=RIGHT>".$taille."</TD><TD WIDTH=200
ALIGN=RIGHT>".$compteurDossierateModif."</TD></TR>\n");
echo ("<TR><TD><A
HREF=".$PHP_SELF."?curr=".$curr.$nom.">".$imgDossier."
".$nom."</A></TD><TD WIDTH=75 ALIGN=CENTER>".$type."</TD><TD
ALIGN=RIGHT>".$taille."</TD><TD WIDTH=200
ALIGN=RIGHT>".$DateModif."</TD></TR>\n");
}
}
}
//}
//}

// Ecriture de la page web
echo ("<H2>".$curr."</H2><B><i>Espace libre : </i></B>
".number_format(diskfreespace($curr)/1000,3,","," ")." Ko<BR>\n");
echo ("<TABLE BORDER=0 CELLSPACING=0 CELLPADING=0>\n<TR
BGCOLOR=#CCCCCC><TD WIDTH=150><A HREF=\"?curr=$curr&tri=n\">Nom</A>
$imgn</TD><TD ALIGN=CENTER><A HREF=\"?curr=$curr&tri=t\">Type</A>
$imgt</TD><TD WIDTH=100 ALIGN=RIGHT><A
HREF=\"?curr=$curr&tri=s\">Taille</A> $imgs</TD><TD WIDTH=200
ALIGN=RIGHT><A HREF=\"?curr=$curr&tri=d\">Dernière modification</A>
$imgd</TD></TR>");
ecrireDossier();
ecrireFichier();
echo ("<TR BGCOLOR=#999999><TD COLSPAN=3 ALIGN=RIGHT><FONT
COLOR=#DDDDDD><B>".number_format($tailleFichiers, 3, ","," ")."
Ko</B></FONT></TD><TD></TD></TR>");
echo ("</TABLE><BR>\n ".$compteurFichier." fichier(s) et
".$compteurDossier." dossier(s). \n");

?>
</BODY>
</HTML>

--
Ceci est une signature automatique de MesNews.
Site : http://arnaud.mesnews.free.fr/

1 réponse

Avatar
P'tit Marcel
Sebounet écrivit
news::

Bonjour, le but du jeu est de lire les dossiers et fichiers d'un
répertoire, de les afficher dans un tableaux, et de pouvoir faire un
tri sur la colonne désirée.
Cependant, vu que c'est un tableau de tableau, j'ai des soucis pour
faire le tri... Si quelqu'un peu m'aider....


Ton article est bien long, tu aurais dû limiter le source aux parties
utiles. Bref j'ai survolé tout ça.


function scanFic($dir){

while ($nomCourant = readdir($fds)){
if (is_file($repCourant.$nomCourant)){
$tabn[$compteurFichier] = $nomCourant;
$tabt[$compteurFichier] =
strtoupper(substr($nomCourant,strrpos($nomCourant,".")+1,strlen($nomCou
rant)-strrpos($nomCourant,".")-1));
$tabs[$compteurFichier] =
number_format((filesize($nomCourant)/1000), 3, ","," ");
$tabd[$compteurFichier] = date("d/m/Y
h:i:s",filemtime($nomCourant));

$tabFichier[$compteurFichier][0]=$tabn[$compteurFichier];
$tabFichier[$compteurFichier][1]=$tabt[$compteurFichier];
$tabFichier[$compteurFichier][2]=$tabs[$compteurFichier];
$tabFichier[$compteurFichier][3]=$tabd[$compteurFichier];
$tailleFichiers+=$tabs[$compteurFichier];
$compteurFichier++;


1°) je vois pas l'intérêt de mettre les données dans 4 tableaux à une
dimension tabn... tabd puis immédiatement dans un tableau à deux dimensions
tabFichier. Garde juste tabn

2°) à la fin, pour trier, il me semble qu'un
array_multisort($tabFichier, $tabn) devrait suffire


eça