Question

I've this simple script for displaying all the images in the folder.

 <?php 

 foreach(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE) as $images) 
{
$filecount = count(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE));
if ($filecount >1)
    {
echo "<img   width='75' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
    }
else
    {
echo "<img   width='200' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
    }

}

 ?>

I want to display number of files in the folder.Giving below what i tried..My problem is that this shows the number of files before each image.

if ($filecount >1)
{
echo '' . $user . ' ' .'added ' . ' ' . $filecount . ' ' . 'new photos';
echo "<img width='75' height='auto' style='margin-right: 3px;  alt='".$row["caption"] ."' src=\"".$images."\">";
}

How can I show number of files before the group of images displayed?

Était-ce utile?

La solution

You need to display the number of files only after it has collected them.
You can do it by declaring the filecount variable before the loop, then you need to count and sum the value everytime the loop goes to the next result, and eventually display the total number of files.
Here is how the code should be written:

<?php 
$filecount = 0;

foreach(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE) as $images) 
{
    $tempFileCount = count(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE))
    $filecount += $tempFileCount;
    if ($tempFileCount > 1)
    {
        echo "<img   width='75' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
    }
    else
    {
        echo "<img   width='200' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
    }
}

if($filecount > 1)
{
    echo '' . $user . ' ' .'added ' . ' ' . $filecount . ' ' . 'new photos';
}

?>

Autres conseils

First create a function that returns all images as array. This will throw an exception if there is an error with the glob php function (will mostly never happen, but worth it to know it when it happens). The function will give the sense that the code is a little bit cleaner.

<?php
function getAllImagesOnDirectory($directory) {
     $imagesArray = glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE);
     if(!is_array($imagesArray)) {
        throw new RuntimeException("There is a problem getting images on directory " . $directory);
     }

     return $imagesArray;
}


//Then we call this function to get all the images.
$allImagesOnFilePath = getAllImagesOnDirectory($filePath);
$numberOfImages = count($allImagesOnFilePath);


if($numberOfImages > 0) { 
     echo '<p>' . $user . ' added ' . $numberOfImages . ' new photos</p>';
     //If the number of images is more than 0 then traverse all the images and echo them
     foreach($allImagesOnFilePath as $oneImage) {
        echo "<img style='max-width:100%;height:auto;margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$oneImage."\">";
     }
}
else { 
     //The user has no uploaded images
     echo '<p>' . $user . ' has not added new photos yet :(</p>';
}

This way we can use the $numberOfImages before using foreach and thus doing what you requested.
We also here use the style attribute to apply width and height to images. See this article to see how to make autoresizable images: http://unstoppablerobotninja.com/entry/fluid-images

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top