...

View Full Version : Sorting file names by date



Dmitry
11-16-2011, 10:56 AM
Hi everyone,

I am having a little issue with sorting file names in a drop down box by date. At the moment the files are arranged in a random manor and its not easy trying to find the right file you are looking for.

We have a website that was designed and coded by a company that no longer exists so they can't help us any longer. The backend of the site is designed to manage products, create/delete and etc etc. Before, when we went to add a new product, it would give us a dropdown box with all the images on the server sorted by date, now since we moved servers, it no longer does this.

So I was wondering if there is something I need to do or maybe change in the coding?

This is the code snippet:


$sServerDir = GetRootPath() . $Config['ProductHiresFilesPath'];
@$oCurrentFolder = opendir( $sServerDir ) ;
while ( @$sFile = readdir( $oCurrentFolder ) )
{
if ( $sFile != '.' && $sFile != '..' && !is_dir( $sServerDir . '/' . $sFile ) )
{
echo "
<option value='$sFile' ".(($row->product_hires_file == $sFile)?"selected='selected'":'').">$sFile</option>\n";
}
}


Any help is appreciated!

Ahlahn
11-16-2011, 02:08 PM
Try echoing this statement in your code


$sServerDir = GetRootPath() . $Config['ProductHiresFilesPath'];
echo $sServerDir;


and make sure that the path matches the path that your files are in. If you changed the name of your folders, that may be an issue.

If it doesn't output the correct url ( or nothing at all), get rid of GetRootPath() and try this:



$sServerDir = $_SERVER['HTTP_HOST'] . $Config['ProductHiresFilesPath'];
echo $sServerDir;


There may also be issues with the $Config (Array of file paths?). But try those steps and let me know what you run into.

Dmitry
11-16-2011, 02:42 PM
Thanks for reply,

Unfortunately that did nothing. The drop-down box still sorts the items randomly and I we need them by date of upload which would make the zip file easier to find.

See the attached image please. Would you like the code for the whole file?

myfayt
11-16-2011, 03:59 PM
Take the part you need out of it, here is a complete one.


<?php

/* Rendering */
function list_dir($path)
{
$items = get_sorted_entries($path);

if (!$items)
return;


foreach($items as $item)
{
if ($item->type=='dir')
{
echo ("<li><img alt='PDF' src='../../../images/Folder.png'>".$item->entry."
\n");
echo "<ul>\n";
list_dir($item->full_path);
}
else
{
echo "<img alt='PDF' src='../../../images/PDF.png'> ".$item->entry."<br/>";
}
}

echo "</ul>";
echo "</li>\n";

}

/* Finding */
function get_sorted_entries($path)
{
$dir_handle = @opendir($path) ;
$items = array();

while (false !== ($item = readdir($dir_handle)))
{
$dir =$path.'/'.$item;
if ( $item == '.' || $item =='..' )
continue;

if(is_dir($dir))
{
$items[] = (object) array('type'=>'dir','entry'=>$item, 'full_path'=>$dir);
}
else
{
$items[] = (object) array('type'=>'file','entry'=>$item, 'full_path'=>$dir);
}
}
closedir($dir_handle);

usort($items,'_sort_entries');

return $items;
}

/* Sorting */
function _sort_entries($a, $B)
{
return strcmp($a->entry,$b->entry);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title>TITLE</title>

<link rel="stylesheet" href="../jquery.treeview.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

<script src="../lib/jquery.cookie.js" type="text/javascript"></script>
<script src="../jquery.treeview.js" type="text/javascript"></script>

<script type="text/javascript" src="demo.js"></script>

</head>
<body>

<div id="treecontrol">
<a title="Collapse the entire tree below" href="#"> Collapse All</a> |
<a title="Expand the entire tree below" href="#"> Expand All</a> |
<a title="Toggle the tree below, opening closed branches, closing open branches" href="#">
Toggle All</a>
</div>

<ul id="red" class="treeview-red">
<?php
list_dir("directory you want to search");
?>
</ul>


<hr />


</body></html>

Dmitry
11-16-2011, 05:24 PM
I am not sure which bit as I am not fluent in php! :(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum