Solved

chose random images for a banner

Posted on 2011-09-12
6
253 Views
Last Modified: 2012-05-12

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
0
Comment
Question by:rgb192
6 Comments
 
LVL 16

Expert Comment

by:santoshmotwani
ID: 36527458
0
 
LVL 8

Accepted Solution

by:
kivan24 earned 500 total points
ID: 36527846
try this:
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36528541
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36528576
Refresh this screen a few times...
http://www.laprbass.com/RAY_temp_rgb192.php

If you have the risk of duplicates in the original $images array, you can use
http://us.php.net/manual/en/function.array-unique.php

Best regards, ~Ray
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

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

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

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

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 36554451
this handles duplicates.
thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36554500
Your choice, of course, but I think I would rather use two statements instead of thirteen.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

919 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

21 Experts available now in Live!

Get 1:1 Help Now