array_multisort to sort each filename against its date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

array_multisort to sort each filename against its date

Post by ozzy »

I dont quite understand how i would get array_multisort (http://uk2.php.net/array_multisort) to sort each filename against its date. The filenames would be collected using glob

Code: Select all

$files = glob($dir . 'Docs/files/*');
Is it possible if anyone could show me or explain to me how. Thanks in advance!
Last edited by ozzy on Mon Apr 17, 2006 1:21 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_multisort() isn't exactly best used to sort a basic one-dimensional array like that returned from glob(). Use sort(), asort(), or usort().
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

function toArray(&$item, $key)
{
   $item = array($item => filemtime($item));
}

array_walk($files, 'toArray');

That could convert all your filesnames to an array with their filemtime(), I believe that is what your after?
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Jcart wrote:

Code: Select all

function toArray(&$item, $key)
{
   $item = array($item => filemtime($item));
}

array_walk($files, 'toArray');

That could convert all your filesnames to an array with their filemtime(), I believe that is what your after?
Thanks Jcart, that's what i was looking for. When i included that code into my script, the following error is returned:

Code: Select all

Warning: basename() expects parameter 1 to be string, array given in /host/localhost/htdocs/dir/index.php on line 55

I know what is wrong, but i dont know what to change to fix it. If you need to see a full copy of my script, i can send it to you via pm.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

post it here
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Here is the full thing:

Code: Select all

<?php
echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">";
$location = "Docs/files/";
$thispage = "modules.php?name=Dir&file=index&";
$dir = getcwd().'/';
$difference = 30;

$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
$end = isset($_GET['end']) ? (int)$_GET['end'] : 0;
if($end <= $start) {
	$end = $start+30;
}

function smart_filemtime($filename) {
  $time = @filemtime($filename);
  if ($time) return $time;
  else return time();
} 

if (is_dir($dir)) {
    $i = 1;
    $files = glob($dir . 'Docs/files/*');
	
	function toArray(&$item, $key)
{
   $item = array($item => filemtime($item));
}

array_walk($files, 'toArray');

    foreach ($files as $path) {
    	$filename = basename($path);
        if($i >= $start && $i != $end ) {
        echo "<tr style='border:1px solid #cfcfcf;background:#fcfcfc; cursor:hand\' onMouseOver=\"this.style.background='#EEEEEE'\" onMouseOut=\"this.style.background='#FFFFFF'\" onClick=\"window.location.href='modules.php?name=Dir&file=file&page=Docs/files/$filename'\">
    <td><img src=\"modules/Dir/images/document.jpg\" width=\"16\" height=\"16\" /> <a href=\"modules.php?name=Dir&file=file&page=Docs/files/$filename\">$filename</a>
	</td>
    <td align=right><a href=\"docs/files/$filename\">Plaintext</a> | <i>$time". date ("F d Y H:i:s.", filemtime($location . $filename));
        echo "</i></td>";
        }
        if($i == $end) break;
        $i++;
    }
}


$back = (($i-$difference) < 0) ? 0 : ($i-$difference);
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference);


echo "<b><div align=\"right\"><a href=\"\" onclick=\"history.go(-1); return false;\">Back</a>\n";
echo " | ";
echo "<a href='".$thispage."start=".$i."&end=".$i."'>Forward</a></div></b>\n";
echo "</table>";
echo "<b><div align=\"right\"><a href=\"\" onclick=\"history.go(-1); return false;\">Back</a>\n";
echo " | ";
echo "<a href='".$thispage."start=".$i."&end=".$i."'>Forward</a></div><br></b>\n";

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$files = glob($dir . 'Docs/files/*');
       
        function toArray(&$item, $key)
{
   $item = array($item => filemtime($item));
}

array_walk($files, 'toArray');

    foreach ($files as $path) {
to

Code: Select all

$files = glob($dir . 'Docs/files/*');
       
    function toArray(&$item, $key)
    {
       $item = date("F d Y H:i:s.", filemtime($key));
    }
    $files = array_flip($files);
    array_walk($files, 'toArray');

    foreach ($files as $path => $filetime) {
and

Code: Select all

date ("F d Y H:i:s.", filemtime($location . $filename))
to

Code: Select all

$filetime
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Hey feyd, thanks for the reply. I applied your changes to my script but it still isnt orderinf it in any particular order.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post your new code.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

My code with the modifications is:

Code: Select all

<table width='100%' border=0 cellspacing=1 cellpadding=0>

<?php
$location = "Docs/files/";
$thispage = "modules.php?name=Dir&";
$dir = getcwd().'/';
$difference = 30;

$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
$end = isset($_GET['end']) ? (int)$_GET['end'] : 0;
if($end <= $start) {
	$end = $start+30;
}

function smart_filemtime($filename) {
  $time = @filemtime($filename);
  if ($time) return $time;
  else return time();
} 

if (is_dir($dir)) {
    $i = 1;
    $files = glob($dir . 'Docs/files/*');
       
    function toArray(&$item, $key)
    {
       $item = date("F d Y H:i:s.", filemtime($key));
    }
    $files = array_flip($files);
    array_walk($files, 'toArray');

    foreach ($files as $path => $filetime) {
    	$filename = basename($path);
        if($i >= $start && $i != $end ) {
        echo "<tr style='border:1px solid #cfcfcf;background:#fcfcfc; cursor:hand\' onMouseOver=\"this.style.background='#EEEEEE'\" onMouseOut=\"this.style.background='#FFFFFF'\" onClick=\"window.location.href='modules.php?name=Dir&file=file&page=Docs/files/$filename'\">
    <td><a href=\"modules.php?name=Dir&file=file&page=Docs/files/$filename\">$filename</a>
	</td>
    <td align=right><a href=\"Docs/files/$filename\">Plaintext</a> | <i>$time". $filetime;
        echo "</i></td>";
        }
        if($i == $end) break;
        $i++;
    }
}


$back = (($i-$difference) < 0) ? 0 : ($i-$difference);
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference);


echo "<b><div align=\"right\"><a href=\"\" onclick=\"history.go(-1); return false;\">Back</a>\n";
echo " | ";
echo "<a href='".$thispage."start=".$i."&end=".$i."'>Forward</a></div></b>\n";
?>
</table>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Nifty, I had always been sorting an array and then using array_reverse(), I guess I overlooked arsort().

[[sorry, off topic]]
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Jcart wrote:arsort()
I was thinking...

Code: Select all

foreach($files as $file)
 {
     $file_array["name"][] = $file;
     $file_array["date"][] = filemtime($file);
 }
 asort($file_array, SORT_NUMERIC);
So i inserted it into my script. The script with above modification:

Code: Select all

location = "Docs/files/"; 
$thispage = "modules.php?name=Dir&file=indexxx2&"; 
$dir = getcwd().'/'; 
$difference = 30; 

$start = isset($_GET['start']) ? (int)$_GET['start'] : 0; 
$end = isset($_GET['end']) ? (int)$_GET['end'] : 0; 
if($end <= $start) { 
    $end = $start+30; 
} 

function smart_filemtime($filename) { 
  $time = @filemtime($filename); 
  if ($time) return $time; 
  else return time(); 
}  

if (is_dir($dir)) 
{ 
    $i = 1; 
    $files = glob($dir . 'Docs/files/*');
    foreach($files as $file) 
    { 
     $file_array[]["name"] = basename($file); 
     $file_array[]["date"] = filemtime($file); 
    } 
    asort($file_array, SORT_NUMERIC);
    foreach ($file_array as $file_info) 
    { 
        if($i >= $start && $i != $end ) 
        { 
        echo "<tr style='border:1px solid #cfcfcf;background:#fcfcfc; cursor:hand\' onMouseOver="this.style.background='#EEEEEE'" onMouseOut="this.style.background='#FFFFFF'" onClick="window.location.href='modules.php?name=Dir&file=file&page=vuns/files/".$file_info["name"]."'"> 
    <td><a href="modules.php?name=Dir&file=file&page=Docs/files/".$file_info["name"]."">".$file_info["name"]."</a> 
    </td> 
    <td align=right><a href="Docs/files/".$file_info["name"]."">Plaintext</a> | <i>".$time.date ("F d Y H:i:s.", $file_info[date]); 
        echo "</i></td>"; 
        } 
        if($i == $end) break; 
        $i++; 
    } 
} 


$back = (($i-$difference) < 0) ? 0 : ($i-$difference); 
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference); 


echo "<b><div align="right"><a href="" onclick="history.go(-1); return false;">Back</a>\n"; 
echo " | "; 
echo "<a href='".$thispage."start=".$i."&end=".$i."'>Forward</a></div></b>\n";
But when i gave it a test all the file are aranged in some strange way, and here is a example of the order of some of the dates:
April 01 2006 17:21:12.
December 31 1969 19:00:00.
April 01 2006 17:21:11.
April 01 2006 17:21:23.
December 31 1969 19:00:00.
April 01 2006 17:21:36.
December 31 1969 19:00:00.
December 31 1969 19:00:00.
April 01 2006 17:21:36.
December 31 1969 19:00:00.
April 01 2006 17:21:39
User avatar
Rivale
Forum Newbie
Posts: 2
Joined: Thu Apr 20, 2006 6:15 am

Post by Rivale »

Looks like something wrong with the timestamp, not sure how you would fix it though.
Post Reply