Jump to content

[SOLVED] Simple Indexing Code


krazytim

Recommended Posts

This is the default code from my installation of wamp. I edited it slighly for the project I am doing, but the basic code is the same.

 

<?php

$list_ignore = array ('.','..','exemples','phpmyadmin','sqlitemanager');
$handle=opendir(".");
$msg = $langues[$langue]['txt_no_projet'];

while ($file = readdir($handle)) {
if (is_dir($file) && !in_array($file,$list_ignore)) {
$msg = '';
echo '<a class="ditem" href="'.$file.'"><img src="dossier.gif" alt="Folder Icon" title="Folder Icon" class="noBorderImg" /> '.$file.'</a><br />
';
}
}
closedir($handle);
echo $msg;

?>

 

When I go to "Localhost" on my computer, everything works fine with this code. But, when I upload the file into a folder I want to index on my server, it no longer puts the folders in alphabetical order. Does anyone know of a way to add to this code to make the indexer render the list in alphabetical order? I know it is possible, because I found some pre-made scripts that did it, but they were too complex with the options. All I want is for the code to echo out that specific code and put it all in order.

 

Thanks in advanced. Sorry if this is a common question/request, I tried to search but I had no luck.

 

Regards,

-Tim

Link to comment
https://forums.phpfreaks.com/topic/77140-solved-simple-indexing-code/
Share on other sites

Just put the results into an array, sort the array and then display the results from the array:

 

<?php

$list_ignore = array ('.','..','exemples','phpmyadmin','sqlitemanager');
$msg = $langues[$langue]['txt_no_projet'];
$folders = array();

//Open the file system object for reading
$handle=opendir(".");

//Iterate through the file objects and put the folders into an array
while ($file = readdir($handle)) {
    if (is_dir($file) && !in_array($file,$list_ignore)) {
        $folders[] = $file;
    }
}

//Close the file system object
closedir($handle);

//Sort the folder array
sort($folders);

//Iterate through the array and display the folders
foreach ($folders as $folder) {
        $msg = '';
        echo "<a class=\"ditem\" href=\"$file\">";
        echo "<img src=\"dossier.gif\" alt=\"Folder Icon\" title=\"Folder Icon\" class=\"noBorderImg\" /> ";
        echo "$file</a><br />";
}

echo $msg;

?>

Try:

<?php

$list_ignore = array ('.','..','exemples','phpmyadmin','sqlitemanager');
$msg = $langues[$langue]['txt_no_projet'];
$folders = array();

//Open the file system object for reading
$handle=opendir(".");

//Iterate through the file objects and put the folders into an array
while ($file = readdir($handle)) {
    if (is_dir($file) && !in_array($file,$list_ignore)) {
        $folders[] = $file;
    }
}

//Close the file system object
closedir($handle);

//Sort the folder array
sort($folders);

//Iterate through the array and display the folders
foreach ($folders as $file) {//note $file not $folder
        $msg = '';
        echo "<a class=\"ditem\" href=\"$file\">";
        echo "<img src=\"dossier.gif\" alt=\"Folder Icon\" title=\"Folder Icon\" class=\"noBorderImg\" /> ";
        echo "$file</a><br />";
}

echo $msg;

?>

Doh!

 

I originally thought you were listing files and not folders when I started working that code. After I reread your post I realized that you were displaying folders and changed the name of the variables to be more appropriate - I forgot to change the variable name used in the echo statements. Personally I would change the foreach to use $folder instead of $file and change the variable in the echo statements.

Thank you guys very much :)

 

I altered the echo statements a bit, but it works perfectly now.

 

If you are curious what this was for, you can see it here: http://timsilva.com/archive/ - Now it does everything I need/want it to do and it fits the main design of my website.

 

Thanks again, these forums are definitely gunna be bookmarked :D

 

-Tim

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.