Jump to content

Randomizing images


artaylor
 Share

Recommended Posts

Hey all,

My client wants a scrolling banner but would like to randomize the images on each page and I am having problems. I have an image field with unlimited images in a site-settings page. I try to shuffle the array returned but they are always in the same order. The code I am using is below. Any and all help is appreciated.

function shuffleAssoc($list) {
    if (!is_array($list)){
        return $list;
    }

    $keys = array_keys($list);
    shuffle($keys);
    $random = array();
    foreach ($keys as $key)
    $random[$key] = $list[$key];

    return $random;
}

function bannerShow() {
	if(count(wire("pages")->get("/site-settings/")->site_banner_images)) {
	    $bannerimages = wire("pages")->get("/site-settings/")->site_banner_images;

	    $out ="";

	    // randomize the images
	    $randimages = shuffleAssoc($bannerimages);
	    
	    foreach ($randimages as $image) {
	        $out .="<img src='$image->url' />/n";
	    }

	    echo $out;
	} else {
		echo "<p>NO BANNER IMAGES FOUND</p>" ;
	}
}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...