Hi!

I have an image slideshow, but it shows first image in alphabetic sequence. I need it shows the fist image, that what has last uploaded in image folder, like sort by date. Sorry for my bad English.

Here is the script

<?php
    $imageDirectory = "images";
    $interval = 5;
?>

<!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=utf-8" />
<title>Slideshow</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var picn=0;
var picList = new Array;
var actImg = new Image();
<?php
if ($handle = @opendir($imageDirectory)) {
    $i = 0;
    while (false !== ($file = readdir($handle)))
    {
        if ($file != "." && $file != "..")
        {
            $path = $imageDirectory;            
            if(is_file($path.DIRECTORY_SEPARATOR.$file)) {
                $info = $path_parts = pathinfo($file);
                if (   (strtolower($info['extension']) == 'jpg') 
                    || (strtolower($info['extension']) == 'jpeg'))  {
                        $file = addslashes($file);
                        echo "picList[$i] = '$path/$file';\r\n";
                        $i++;
                    }
            } 
         }
    }
}
?>

function setCountDown ()
{
  actImg.src = picList[picn++];
  document.getElementById("pic").src=actImg.src;
  if (picn>=picList.length) picn=0;

  setTimeout ( "setCountDown()", <?php echo ($interval*100000); ?> );
}

var imgNum = 0;
$("#0").fadeIn(300);


function prev () {
    actImg.src = picList[picn--];
    document.getElementById("pic").src=actImg.src;
    imgNum = newImageNum;
}

function next () {
   actImg.src = picList[picn++];
    document.getElementById("pic").src=actImg.src;
    imgNum = newImageNum;
}

</script>
</head>
<div class="gallery-controls">
    <button onclick="prev()">Previous</a>
    <button onclick="next()">Next</a>
</div>
<body onload="setCountDown();">
<div id="container">
    <div id="header"><div id="header_left"></div>
    <div id="header_main">SlideShow</div><div id="header_right"></div></div>
    <div id="content">
        <img src="<?php echo $path.'/'.$file; ?>" alt="pic" id="pic" border="1" />
    </div>
    <div id="footer"><a href="" target="_blank"></a></div>
</div>
</body>
</html>

Thanks!

Member Avatar for diafol

SLightly changes your code to match a quick solution with my directory:

$imageDirectory = "./";

if ($handle = @opendir($imageDirectory)) {
    $i = 0;
    while (false !== ($file = readdir($handle)))
    {
        if ($file != "." && $file != "..")
        {
            $path = $imageDirectory;            
            if(is_file($path.$file)) {
                $info = strtolower(pathinfo($file, PATHINFO_EXTENSION));
                if (in_array($info,array('png','jpg','php','ico'))){
                        $files[] = $path.$file;

                }
            } 
         }
    }
}


echo "<h3>Alphabetical</h3><pre>";
print_r($files);
echo "</pre>";
$x = array_multisort(array_map('filemtime', $files), SORT_DESC, $files);
echo "<h3>By Date</h3><pre>";
print_r($files);
echo "</pre>";

So, use of array_multisort and array_map. They're not the quickest methods, but as long as you don't have thousands of images, should be ok. Use SORT_ASC if you want that order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.