Solved

if more images are called

Posted on 2011-09-17
9
271 Views
Last Modified: 2012-05-12
see related question

want to chose random images for a banner  with no duplicate images



function chose(image inventory, number of images to pack in return array){}


$images = array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg', '7.jpg', '8.gif');
$imageSet = chose($images, 4);
echo $imageSet





the code does that but if more images are called (2nd paramater) than are available only use available
function chose($img_arr, $img_num) {
    $rand_img = array();
    $img_array_len = count($img_arr);
    for ($i=0; $i<$img_num; $i++) {
        $randindex = rand(0, $img_array_len-1);
        if (!in_array($img_arr[$randindex], $rand_img)) {
            $rand_img[] = $img_arr[$randindex];
        } else {
            $i--;
        }
    }
    return $rand_img;
}


$images = array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg', '7.jpg', '8.gif');
$imageSet = chose($images, 4);
print_r ($imageSet);

Open in new window

0
Comment
Question by:rgb192
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 10

Expert Comment

by:acbxyz
ID: 36554498
Between line 3 and 4, you can insert one more line to avoid an endless loop::
if ($img_num >= $img_array_len) return $img_arr;

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 36554508
>>but if more images are called (2nd paramater) than are available only use available
function chose($img_arr, $img_num) {
    if( $img_num >= count($img_arr) ){return $img_arr;}
    $rand_img = array();
...
}
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36554512
The code posted with this question will not work:

$images = array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg', '7.jpg', '8.gif');
$imageSet = chose($images, 4);
echo $imageSet

$imageSet is an array.  echo is unlikely to produce anything useful.

The correct technique is still the same as the one I posted in the other question, and as shown in this code snippet.  I'll leave it as an exercise for others to package this into a function.

Good luck with your project, ~Ray
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA
$images = array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg', '7.jpg', '8.gif', '8.gif');

// UNIQUE NAMES ONLY
$images = array_unique($images);

// GET FOUR AT RANDOM
shuffle($images);
$chosen = array_slice($images , 0, 4);

// SHOW THE WORK PRODUCT
print_r($chosen);

// TRY TO GET THIRTEEN AT RANDOM
shuffle($images);
$chosen = array_slice($images , 0, 13);

// SHOW THE WORK PRODUCT
print_r($chosen);

Open in new window

0
 

Author Comment

by:rgb192
ID: 36555122
ray is correct also, I just dont know how to put in function


question for acbxyz, hielo


    if( $img_num >= count($img_arr) ){return $img_arr;}

if ($img_num >= $img_array_len) return $img_arr;


works but does not do random order
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:acbxyz
ID: 36555149
Multiple possibilities:
1) between line 3 and 4
if ($img_num >= count($img_array_len)) {
  shuffle($img_arr);
  return $img_arr;
}

Open in new window

2) between 7 and 8
} else if (count($rand_img) == $img_array_len) {
  return $rand_img;
}

Open in new window

3) Based on Ray's advice, and I must admit, this would be my choice
function chose($img_arr, $img_num) {
    $arr2 = array_unique($img_arr); // side effect: be shure we don't have a by reference variable
    shuffle($arr2);
    return array_splice($arr2, 0, $img_num);
}

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36555165
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36555171

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA
$images = array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg', '7.jpg', '8.gif', '8.gif');

// SHOW HOW TO USE THE FUNCTION
print_r(image_array($images));
print_r(image_array($images, 4));
print_r(image_array($images, 713));

// DEFINE THE FUNCTION
function image_array($images, $num=1)
{
    // UNIQUE NAMES ONLY
    $images = array_unique($images);

    // GET A RANDOM COLLECTION
    shuffle($images);
    return array_slice($images, 0, $num);
}

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 36556055
now this is the best answer.
thank you
0
 

Author Comment

by:rgb192
ID: 36565139
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now