/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Sort an array

Hey Everyone,

I’ve got a great dynamic image gallery going on my website and I was wondering if I could get some help sorting this array. I have it displaying all directories within a folder as links in a list. The folders for my albums are all following this naming structure: Month Year (ex. April 2006). Is there anyway to sort the array so I can break up the albums by year. For example I would have all albums with the word 2004 displayed within one <div> or <ul>. And then have all the albums with the word 2005 displayed in another <div> or <ul>. Here is the code I am using right now to display my folders.

[code=php]
<?php
$albumsDir=”ajax/albums/”;
function dirList ($directory){
$results = array();
$handler = opendir($directory);
while ($file = readdir($handler)) {
if ($file != ‘.’ && $file != ‘..’)
$results[] = $file;
}
closedir($handler);
return $results;
}
$photolist = (dirList(“./”.$albumsDir));
for ($i=0;$i<count($photolist);$i++)
echo “<li><a title=”$photolist[$i]” target=”iframemain” href=”http://www.giannaross.com/ajax/?album=”.$photolist[$i].””>”.$photolist[$i].”</a></li>n”;

?>
[/code]

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@balloonbuffoonMay 03.2006 — Try this out:[code=php]$albumsDir="ajax/albums/";
for($i=1;$i<=12;$i++) {
$months[$i] = date("F",mktime(0,0,0,$i,1));
}
$photolist = glob($_SERVER["DOCUMENT_ROOT"].$albumsDir."*",GLOB_ONLYDIR);
foreach($photolist as $photo) {
$mY = explode(" ",$photo);
$photos[$mY[1]][array_search($mY[0], $months)] = true;
}
ksort($photos,SORT_NUMERIC);
foreach($photos as $key => $photo) {
ksort($photo,SORT_NUMERIC);
echo "<div>$key Albums<ul>";
foreach($photo as $mon => $t) {
$text = $months[$mon]." ".$key;
echo "<li><a title='$text' target='iframemain' href='http://www.giannaross.com/ajax/?album=".urlencode($text)."'>$text</a></li>n";
}
echo "</ul></div>";
}[/code]
--Steve
Copy linkTweet thisAlerts:
@balloonbuffoonMay 03.2006 — As a side note, in your code:
[list]
  • [*]Your dirList() function is identical to php's native [url=http://us2.php.net/manual/en/function.glob.php]glob()[/url] function. (See how it is used in the script above and also examples in the manual.)

  • [*]This:[code=php]for ($i=0;$i<count($photolist);$i++)
    echo "<li><a title="$photolist[$i]" target="iframemain" href="http://www.giannaross.com/ajax/?album=".$photolist[$i]."">".$photolist[$i]."</a></li>n"; [/code]
    could have been achieved using [url=http://us2.php.net/manual/en/control-structures.foreach.php]foreach()[/url]:[code=php]foreach ($photolist as $photo) {
    echo "<li><a title="$photo" target="iframemain" href="http://www.giannaross.com/ajax/?album=".$photo."">".$photo."</a></li>n";
    }[/code]

  • [/list]
    --Steve
    Copy linkTweet thisAlerts:
    @phunk311authorMay 03.2006 — Thanks for the help. I got an error though. If you can help great, if not don't worry about it. My folders are sorting by date right now and I can live with that.

    Warning: ksort() expects parameter 1 to be array, null given
    Copy linkTweet thisAlerts:
    @balloonbuffoonMay 03.2006 — What line number did that warning give?

    --Steve
    Copy linkTweet thisAlerts:
    @phunk311authorMay 03.2006 — It was line 18, so here is the entire page so you know what line it is.

    [code=php]<!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>Untitled Document</title>
    </head>
    <body>
    <?php
    $albumsDir="./ajax/albums/";
    for($i=1;$i<=12;$i++) {
    $months[$i] = date("F",mktime(0,0,0,$i,1));
    }
    $photolist = glob($_SERVER["DOCUMENT_ROOT"].$albumsDir."*",GLOB_ONLYDIR);
    foreach($photolist as $photo) {
    $mY = explode(" ",$photo);
    $photos[$mY[1]][array_search($mY[0], $months)] = true;
    }
    ksort($photos,SORT_NUMERIC);
    foreach($photos as $key => $photo) {
    ksort($photo,SORT_NUMERIC);
    echo "<div>$key Albums<ul>";
    foreach($photo as $mon => $t) {
    $text = $months[$mon]." ".$key;
    echo "<li><a title='$text' target='iframemain' href='http://www.giannaross.com/ajax/?album=".urlencode($text)."'>$text</a></li>n";
    }
    echo "</ul></div>";
    }
    ?>
    </body>
    </html>[/code]
    Copy linkTweet thisAlerts:
    @balloonbuffoonMay 03.2006 — Can you put these lines right above "ksort($photos,SORT_NUMERIC);" (for debugging purposes):
    [code=php]echo "<pre>";
    print_r($photolist);
    print_r($photos);
    "</pre>";[/code]
    ...and then post what the page displays.

    --Steve
    Copy linkTweet thisAlerts:
    @phunk311authorMay 03.2006 — Array

    (

    )

    Warning: ksort() expects parameter 1 to be array, null given in /home/content/p/h/u/phunk311/html/what.php on line 22
    Copy linkTweet thisAlerts:
    @balloonbuffoonMay 03.2006 — Ok, I think I know what the problem might be.

    Change:[code=php]$albumsDir="./ajax/albums/";[/code]to[code=php]$albumsDir="/ajax/albums/";[/code]Try that out.

    --Steve
    Copy linkTweet thisAlerts:
    @phunk311authorMay 04.2006 — Array

    (

    [0] => /home/content/p/h/u/phunk311/html/ajax/albums/April 2005

    [1] => /home/content/p/h/u/phunk311/html/ajax/albums/April 2006

    [2] => /home/content/p/h/u/phunk311/html/ajax/albums/August 2004

    [3] => /home/content/p/h/u/phunk311/html/ajax/albums/August 2005

    [4] => /home/content/p/h/u/phunk311/html/ajax/albums/December 2004

    [5] => /home/content/p/h/u/phunk311/html/ajax/albums/December 2005

    [6] => /home/content/p/h/u/phunk311/html/ajax/albums/February 2005

    [7] => /home/content/p/h/u/phunk311/html/ajax/albums/February 2006

    [8] => /home/content/p/h/u/phunk311/html/ajax/albums/January 2005

    [9] => /home/content/p/h/u/phunk311/html/ajax/albums/January 2006

    [10] => /home/content/p/h/u/phunk311/html/ajax/albums/July 2004

    [11] => /home/content/p/h/u/phunk311/html/ajax/albums/July 2005

    [12] => /home/content/p/h/u/phunk311/html/ajax/albums/June 2005

    [13] => /home/content/p/h/u/phunk311/html/ajax/albums/March 2005

    [14] => /home/content/p/h/u/phunk311/html/ajax/albums/March 2006

    [15] => /home/content/p/h/u/phunk311/html/ajax/albums/May 2005

    [16] => /home/content/p/h/u/phunk311/html/ajax/albums/November 2004

    [17] => /home/content/p/h/u/phunk311/html/ajax/albums/November 2005

    [18] => /home/content/p/h/u/phunk311/html/ajax/albums/October 2004

    [19] => /home/content/p/h/u/phunk311/html/ajax/albums/October 2005

    [20] => /home/content/p/h/u/phunk311/html/ajax/albums/September 2004

    [21] => /home/content/p/h/u/phunk311/html/ajax/albums/September 2005

    )

    Array

    (

    [2005] => Array

    (

    [0] => 1

    )

    [2006] => Array
    (
    [0] => 1
    )

    [2004] => Array
    (
    [0] => 1
    )


    )

    2004 Albums

    * 2004

    2005 Albums

    * 2005

    2006 Albums

    * 2006
    Copy linkTweet thisAlerts:
    @balloonbuffoonMay 04.2006 — Ok, that solved one problem. Now to try soving the next:

    Try replacing:
    [code=php]$mY = explode(" ",$photo);[/code]with:[code=php]$mY = explode(" ",basename($photo));[/code]--Steve
    Copy linkTweet thisAlerts:
    @phunk311authorMay 04.2006 — Steve,

    That fixed it. Thanks for the help!

    Anthony
    Copy linkTweet thisAlerts:
    @balloonbuffoonMay 04.2006 — No problem!

    --Steve
    ×

    Success!

    Help @phunk311 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.27,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...