Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

if more images are called

Posted on 2011-09-17
9
Medium Priority
?
279 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 111

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
Technology Partners: 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 111

Expert Comment

by:Ray Paseur
ID: 36555165
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

715 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