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
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;

?>

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.