0
\$\begingroup\$

I made a function about random backgrounds

And those images will unique as possible. (Shuffle again after all gone)

My steps is :

  • Setup the array
  • Shuffle it
  • Do function (check if isset() so run on)

PHP :

$rand_image_ids = [1048,1049,1061,1064,983,972,964];
shuffle($rand_image_ids);
$rand_images_i = 0;

function rand_image($width,$height){
    global $rand_image_ids,$rand_images_i;
    if( !isset($rand_image_ids[$rand_images_i]) ) {
        shuffle($rand_image_ids);
        $rand_images_i = 0;
    }
    $id = $rand_image_ids[$rand_images_i];
    $rand_images_i++;
    return 'https://unsplash.it/'.$width.'/'.$height.'?image='.$id;
}

So I am looking for :

  • Bugs ?
  • Any better way to do ?
  • Shorter codes if possible

Possible with PHP 5.6

\$\endgroup\$
1
\$\begingroup\$

There is one bug, in that the function doesn't quite do what you want it to. It does not loop though the randomized array, persisting the order until you've reached the end. As you seem to want from your description. What it does do is to randomize the array once, then once more if there is no array index 0 (which is only true if the array is empty). Then it retrieves the first image ID from the array, increases the counter, generates the URL. Before it finally promptly forgets everything done inside the function, except the finalized URL.

If you want true persistence on the order of images, then you need to store it somewhere. Either in the session, or in a database.

As for the last two questions, I've refactored your code to be more in line with recommended practices:

function randomImageLink ($width, $height, $images) {
    // Using mt_rand () instead of array_rand () due to randomization issues.
    $loc = mt_rand (0, count ($images) - 1);

    // Get a random image ID from the array.
    $id = $images[$loc];

    return 'https://unsplash.it/' . $width . '/' . $height . '?image=' . $id;
}

$imageList = [1048, 1049, 1061, 1064, 983, 972, 964];
$imageLink = randomImageLink (800, 600, $imageList);

Note that this is based upon the function you wrote, not the function you wanted. It should, however, work as an example on how you can restructure your code in general.

PS: I've assumed that the list of image IDs come from a external source, such as a "glob()" on a folder or a DB query.

\$\endgroup\$
  • \$\begingroup\$ Thanks, But one thing.. Is this possible to prevent/remove $imageList when call the function every time ? \$\endgroup\$ – l2aelba Apr 8 '16 at 10:07
  • 1
    \$\begingroup\$ Yes, you can have the code that reads the list from the external source inside the function itself. The easiest way to solve it is to create a function that reads and returns the list, and then call it inside the randomImageLink () function. Having a dedicated function for that should also help you a lot with the persistence of the array order. \$\endgroup\$ – ChristianF Apr 8 '16 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.