Jump to content


Photo

Periodically changing random images?


  • Please log in to reply
23 replies to this topic

#21 fishbowl

fishbowl
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 August 2006 - 02:18 AM

Okay, it's still not changing the images. The new while() loop you provided didn't look different from the original while() loop, other than the "// boot out if we've reached the limit of images" section of code being removed. I also inserted the new 2 lines.

Well, just for reference, this is what it looks like so far. It's still showing the same 20 images.

<?php

function get_images_list($list_filename, $path_to_images, $number_of_images)
{
  // define an array of image extensions
  $image_extensions = array('gif');

  // initialise the return variable
  $image_list = array();

  // check if the list file exists
  if (file_exists($list_filename))
  {
    // include it so that it defines an array for us (called $image_list by default)
    include($list_filename);

    // check if the list is less than 24 hours old
    if ($image_list['timestamp'] >= (time() - 10))
    {
      // if it is less than 24 hours old, just drop the timestamp and return the list
      unset($image_list['timestamp']);
      return $image_list;
    }
    else
    {
      // if it's stale, then undo the definition the include has made
      unset($image_list);
    }
  }

  // by now it will have booted out of the function if the file's image list was useable
  // therefore if we're still here, we need to create a new file list (either for the first time, or to replace the stale one)

  // open the images directory
  $handle = @opendir($path_to_images);

  // boot out with a NULL return if the directory couldn't be opened
  if ($handle === FALSE)
    return NULL;

  // otherwise, go through the directory
  while ($filename = readdir($handle))
  {
    // grab a lowercase version of its extension
    $extension = strtolower(substr(strrchr($filename, '.'), 1));

    // if its extension is an image type, then log it
    if (in_array($extension, $image_extensions))
      $image_list[] = $filename;

  }

  // close the directory
  closedir($handle);

$chunks = array_chunk($image_list, $number_of_images);
$image_list = $chunks[0];

  // add a timestamp to the array
  $image_list = array_merge($image_list, array('timestamp' => time()));

  // turn the list content into something useable
  $content = '<?php $image_list = '.var_export($image_list, TRUE).'; ?>';

  // write the list to a file
  $newfile = fopen($list_filename, 'w');
  fwrite($newfile, $content);
  fclose($newfile);

  // kill the timestamp again
  unset($image_list['timestamp']);

  // return NULL if it found no images, or the image list if it did
  return (empty($image_list)) ? NULL : $image_list;
}

$image_list = get_images_list('image_list.php', 'images/', 20);

if ($image_list === NULL || empty($image_list))
{
  echo 'No images could be grabbed.';
}
else
{
foreach ($image_list AS $filename)
{
  echo '<img src="images/'.$filename.'" />';
}
}
?>


#22 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 August 2006 - 03:11 AM

again, i'm a dummy.  add this line before the chunks line:

shuffle($image_list);

hopefully that patches it up.

#23 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 13 August 2006 - 03:52 AM

Thats because you might have your cache send back the same stuff.
Its better that you refresh twice or put cache control in your headers
I am a PHP Guru, ask me questions if you want to

#24 fishbowl

fishbowl
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 August 2006 - 04:21 AM

hopefully that patches it up.

It worked!! This is awesome. Thanks so much for all the help. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users