Solved

if more images are called

Posted on 2011-09-17
9
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 110

Expert Comment

by:Ray Paseur
ID: 36555165
0
 
LVL 110

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

628 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