Welcome to WebmasterWorld Guest from 54.197.168.70

Forum Moderators: coopster & jatar k

Order of images in script

   
2:41 pm on Jul 21, 2012 (gmt 0)

5+ Year Member



I found this bit of code which brings in images from a chosen folder so that I can create a slideshow from it. The problem is the images don't seem to be coming in any kind of order - It appears totally random i.e. 01,04,03 rather than what I would like of 01,02,03

Any ideas or help would be greatly appreciated


<?php
$directory = 'images/slideshow';
try {
// Styling for images
echo "<div id=\"main\">";
foreach ( new DirectoryIterator($directory) as $item ) {
if ($item->isFile()) {
$path = $directory . "/" . $item;
echo "<img src=\"" . $path . "\" />";
}
}
echo "</div>";
}
catch(Exception $e) {
echo 'No images found for this slideshow.<br />';
}
?>
2:01 am on Jul 22, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're right. File systems' directories do not store the file names in any sorted order. I'm sure there's an explanation for how they do store them, but it doesn't matter. If you want to deal with files in a sorted manner (by file name, last modification date, creation date, or whatever), you need to do the sorting on your own. This generally means reading all of the file names into an array and then sorting that array as needed, as in something like:

<?php
$directory = 'images/slideshow';
try {
// Styling for images
echo "<div id=\"main\">";
$slidefiles = new DirectoryIterator($directory);
asort($slidefiles);
foreach ($slidefiles as $item ) {
if ($item->isFile()) {
$path = $directory . "/" . $item;
echo "<img src=\"" . $path . "\" />";
}
}
echo "</div>";
}
?>
8:18 pm on Jul 22, 2012 (gmt 0)

5+ Year Member



Thanks for your help with this. I inserted your code but I get an error:

Parse error: syntax error, unexpected ';', expecting T_CATCH in /home/temp/public_html/dev/index.php on line 40

I tried the original code again and removed


catch(Exception $e) {
echo 'No images found for this slideshow.<br />';
}


and got the same error.
2:45 am on Jul 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see any problems with the code I posted, but without being able to see your entire page/script I can't tell what line #40 looks like to try to find the problem. It probably doesn't matter, though, because the only critical change required to sort the file list is to replace the line:

foreach ( new DirectoryIterator($directory) as $item ) {

in your original script with:

$slidefiles = new DirectoryIterator($directory);
asort($slidefiles);
foreach ($slidefiles as $item ) {
2:10 pm on Jul 23, 2012 (gmt 0)

5+ Year Member



Hi. Thanks once agin for your relpy…

I'm now getting the following error:


Warning: asort() expects parameter 1 to be array, object given in /home/mysite/public_html/dev/index.php on line 31


Here's the actual page code - Not really sure where to begin!?


<!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></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="scripts/jquery.cycle.all.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#main').cycle({
fit: 1, pause: 2
});
});
</script>
<link rel="stylesheet" href="reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="container">
<div id="header">
<div id="logo"></div>
</div>
<?php
$directory = 'images/slideshow';
try {
// Styling for images
echo "<div id=\"main\">";
$slidefiles = new DirectoryIterator($directory);
asort($slidefiles);
foreach ($slidefiles as $item ) {
if ($item->isFile()) {
$path = $directory . "/" . $item;
echo "<img src=\"" . $path . "\" />";
}
}
echo "</div>";
}
catch(Exception $e) {
echo 'No images found for this slideshow.<br />';
}
?>
<div id="footer">
<div class="details aleft">content</div>
<div class="icon"></div>
<div class="details aright">content</div>
<div class="clear"></div>
</div>
</div>
</div>
</body>
<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', '']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>
</html>
2:48 pm on Jul 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry... I hadn't worked with the DirectoryIterator class and thought it was a local function. Replace:

$slidefiles = new DirectoryIterator($directory);
asort($slidefiles);
foreach ($slidefiles as $item ) {
if ($item->isFile()) {
$path = $directory . "/" . $item;
echo "<img src=\"" . $path . "\" />";
}
}

with:

$slidefiles = array();
$iterator = new DirectoryIterator($directory);
$x = 0;
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
$slidefiles[$x++] = $fileinfo->getFilename();
}
}
asort($slidefiles);
foreach ($slidefiles as $item ) {
$path = $directory . "/" . $item;
echo "<img src=\"" . $path . "\" />";
}
2:58 pm on Jul 23, 2012 (gmt 0)

5+ Year Member



You are a genius... Thanks sooooo much for your help with this - really appreciated :-)
3:20 pm on Jul 24, 2012 (gmt 0)



rainborick wrote:
I don't see any problems with the code I posted [...]

You had a
try
block without a
catch
block ("expecting T_CATCH").

--
Ryan
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month