Jump to content

[SOLVED] Order array of files made with glob()


ashton321

Recommended Posts

Hi,

 

I have an array of file names to be used in another script created using glob() is there anyway to order the list by the date modified on the server?

 

<?php $query = mysql_query("SELECT * FROM galleries");
                        $result = mysql_fetch_array($query);
                        $galname = $result['galname'];
                        $files = array();
                                foreach(glob("../webdev/images/galleries/$galname/*") as $file)
                                {
                                        if(!is_dir($file))
                                        {
                                                $files[] = array("path" => $file);
                                        }
                                }
                        
                        // echo $files[0]["name"];
                        $firstfile = @ltrim($files[0]["path"], "../webdev/");
                        $firstfile = '../'.$firstfile;
                        echo '<a href="../scripts/phpThumb.php?src='.$firstfile.'&h=400&w=400" rel="lightbox-'.$galname.'" ><img src="../scripts/phpThumb.php?src='.$firstfile.'&h=200&w=200"</a>';
                        foreach ($files as $pic)
                        {
                                foreach ($pic as $path)
                                {
                                        if ($path != $files[0]["path"])
                                        {                                        
                                        $path =  @ltrim($path, "../webdev/");
                                        $path = '../'.$path;
                                        echo '<a href="../scripts/phpThumb.php?src='.$path.'&h=400&w=400" rel="lightbox-'.$galname.'" ></a>';
                                        }
                                        else {}
                                }        
                        }  ?>

Not tested:

<?php

$query = mysql_query("SELECT * FROM galleries");

$result = mysql_fetch_array($query);
$galname = $result['galname'];

$files = array();
foreach(glob('../webdev/images/galleries/$galname/*') as $file)
{
    if(is_file($file))
    {
        $files[] = array(
            'path'          => '../' . ltrim($file, '../webdev/'),
            'date_modified' => filemtime($file);
        );
    }
}
                    
//Order the array by date modified
function sortByModified()
{
    if ($a['date_modified'] == $b['date_modified']) {
        return 0;
    }
    return ($a['date_modified'] < $b['date_modified']) ? -1 : 1;
}
usort($files, 'sortByModified');

//print the results
echo "<a href=\"../scripts/phpThumb.php?src={$files[0]['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" >
echo "<img src=\"../scripts/phpThumb.php?src={$files[0]['path']}&h=200&w=200\"</a>";

foreach ($files as $file)
{
    echo "<a href=\"../scripts/phpThumb.php?src={$file['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" ></a>";
}

?>

Ok, I understand that(should have looked for the filetime function), now how would i apply that to my foreach loop that displays the result?  I know if it were SQL you would date ORDER by date_modified DESC but i am assuming its not that simple for a foreach loop.  I am just beginning to use foreach loops in my coding so im not exactly sure what to do.

 

here is how i display the result currently

<?php $firstfile = @ltrim($files[0]["path"], "../webdev/");
                        $firstfile = '../'.$firstfile;
                        echo '<a href="../scripts/phpThumb.php?src='.$firstfile.'&h=400&w=400" rel="lightbox-'.$galname.'" ><img src="../scripts/phpThumb.php?src='.$firstfile.'&h=200&w=200"</a>';
                        foreach ($files as $pic)
                        {
                                foreach ($pic as $path)
                                {
                                        if ($path != $files[0]["path"])
                                        {
                                        $path =  @ltrim($path, "../webdev/");
                                        $path = '../'.$path;
                                        echo '<a href="../scripts/phpThumb.php?src='.$path.'&h=400&w=400" rel="lightbox-'.$galname.'" ></a>';
                                        }
                                        else {}
                                }
                        } ?>

In the code I provided, it would create a multi-dimensional array for each file which would include the path and the file modified date. Then there is a small block of code that would order the array. Then you just do a foreach loop to echo the results.

 

As I stated that code was not tested - but the logic was sound. I fixed a few minor errors and it works fine now. Give this a try:

 

<?php

$query = mysql_query("SELECT * FROM galleries");

$result = mysql_fetch_array($query);
$result['galname'];

$files = array();
foreach(glob("../webdev/images/galleries/$galname/*") as $file)
{
    if(is_file($file))
    {
        $files[] = array(
            'path'          => $file,
            'name'          => basename($file),
            'date_modified' => filemtime($file)
        );
    }
}

//Order the array by date modified
function sortByModified()
{
    if ($a['date_modified'] == $b['date_modified']) {
        return 0;
    }
    return ($a['date_modified'] < $b['date_modified']) ? -1 : 1;
}
usort($files, 'sortByModified');

//print the results
echo "<a href=\"../scripts/phpThumb.php?src={$files[0]['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" >";
echo "<img src=\"{$files[0]['path']}\"</a><br /><br />\n";

foreach ($files as $file)
{
    echo "<a href=\"../scripts/phpThumb.php?src={$file['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" >{$file['name']}</a><br />\n";
}

?>

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.