Solved

chose random images for a banner

Posted on 2011-09-12
6
251 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
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 …

744 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

12 Experts available now in Live!

Get 1:1 Help Now