I could use some assistance in trying to streamline this jumble of code into maybe a function, or at least slimming this down some.

with this code I am counting the amount of images in a folder (4 folders actually) and then getting that result, and supposedly adding one to it, so therefor when my other code puts an image into that folder it'll be given the new count (so if theres 2 in there, then putting a new fall picture in there would make it fall3, etc)

// this counts the season folders
$fallcnt=0;
$falldirname="images/fall";
$falldn=opendir($falldirname);
while ($dircount=readdir($falldn))
{
$fallcnt=$fallcnt+1;
}
closedir ($falldn);

$fallcount=$fallcnt-1;

$wintercnt=0;
$winterdirname="images/winter";
$winterdn=opendir($winterdirname);
while ($dircount=readdir($winterdn))
{
$wintercnt=$wintercnt+1;
}
closedir ($winterdn);

$wintercount=$wintercnt-1;

$summercnt=0;
$summerdirname="images/summer";
$summerdn=opendir($summerdirname);
while ($dircount=readdir($summerdn))
{
$summercnt=$summercnt+1;
}
closedir ($summerdn);

$summercount=$summercnt-1;

$springcnt=0;
$springdirname="images/spring";
$springdn=opendir($springdirname);
while ($dircount=readdir($springdn))
{
$springcnt=$springcnt+1;
}
closedir ($springdn);

$springcount=$springcnt-1;
// end folder count

Recommended Answers

All 5 Replies

function count_dir($dirname)
{
  return count(array_slice(scandir($dir), 2));
}

$seasons = array('spring', 'summer', 'winter', 'fall');
$springcount = $summercount = $fallcount = $wintercount = 0;
foreach ($seasons as $season) {
  $seasoncnt = $season . 'count';
  $$seasoncnt = count_dir("images/" . $season);
}
// done

Whoops: For Reference Variable Variables, http://php.net/scandir, http://php.net/array_slice

ah hah, i'll test that out and let you know the results

hmm, got a undefined function

Fatal error: Call to undefined function: scandir() in

im betting the $dir which isnt assigned to anything

No, that means you're using PHP4 which is quite unfortunate.

if (!function_exists('scandir')) {
  function scandir($path, $sort = 0)
  {
    if (!$dir = opendir($path)) {
      return array();
    }
    $files = array();
    while (false !== ($file = readdir($dir))) {
      if ($file != '.' && $file != '..') {
        $files[] = $file;
      }
    }
    closedir($dir);
    return $files;
  }
}

yeh, i keep telling my boss we need to host all this stuff ourselves, and keep our own php and everything updated, but what can ya do, hes stubborn and likes godaddy

i'll test this new function out, and if it works i'll give you a $_COOKIE... geek humor is fun

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.