Non Repeated Background Images On Refresh or New Page - Array Reset When Empty

At the moment this code is simply picking a random background image from the array and setting it for each new page I navigate. I want a unique background image for each page refresh or each time I click to a new page without the same image being repeated until the array is empty. Currently the array is being reset each time I refresh/navigate and I am seeing some repeated backgrounds before all images in the array have been ‘used’. The array should reset only after all 7 backgrounds have been uniquely sampled. The location of my code is up the top of my ‘head’ html file which is being used across all pages using php ‘includes’. I’m thinking I must need to use a session but Im not sure. Any help please?

<?php
  $backgrounds = $background_history = array('one.jpg', 'two.jpg', 'three.jpg', 'four.jpg', 'five.jpg', 'six.jpg', 'seven.jpg');
  
  $result = "";
  for ($i = 1; $i < 8; $i++) {
      if (empty($background_history)) {
          $background_history = $backgrounds;
      }
     $key = array_rand($background_history, 1);
     $selected = $background_history[$key];
     unset($background_history[$key]);
     $result = $selected;
 }
 ?>
 
 <!DOCTYPE html>
 <html lang="en">
 <head>
 ...etc...etc...etc
 
     <style>
         .all-backgrounds {
             background: linear-gradient(rgba(0, 0, 0, 0.45), rgba(0, 0, 0, 0.45)), url(../static/backgrounds/<?php echo $result ?>) no-repeat center center;
             -webkit-background-size: cover;
             -moz-background-size: cover;
             background-size: cover;
             -o-background-size: cover;
             height: 100%;
         }
     </style>

Hi @twelvemoons1979 and a warm welcome to the forums.

One way to get images that cycle from 0…count(images)-1; and to use a $_SESSION is the following:

# Start $_SESSION
  session_start();
  if( isset( $_SESSION['iImg'] ) ):
    # increment 
      $_SESSION['iImg'] = 1 + (int) $_SESSION['iImg'];    
  else:
      $_SESSION['iImg'] = 0;    
  endif;

  # GET images from a directory 
    $thumbs = glob('/home/john/www/THUMB-KLUDGE/*.jpg');
    $thumbs = array_slice($thumbs, 0, 19);

# Set next image 
  $_SESSION['iImg'] = (int) $_SESSION['iImg'] % count($thumbs);
  $result = $thumbs[ $_SESSION['iImg'] ];
  $result = str_replace('/home/john/www/', 'http://localhost/', $result);

Please note that arrays start at 0;

Thank you so much John that’s a much better way of doing it. I have it working perfectly. Cheers!

1 Like

Actually sorry for the re-post but could you please explain why the very last line of code:
$result = str_replace(‘/home/john/www/’, ‘http://localhost/’, $result);
is needed?

It still works even if after i comment this line out.

Thanks again.

1 Like

Personal preference, I prefer others who view the source code to see a URL and not the full server path. Maybe I am little paranoid :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.