Jump to content

multiple random images from same array


arumdevil

Recommended Posts

Hi all...

 

I am new to the forum and new to PHP, so be gentle. I have been searching the web for this all day so forgive me if I have missed a post that answers my question.

 

I want to display 4 images (but with possibility of more in future) on a page which are randomly selected from a number of possible images. at the moment I have each possible image as a list in an array, and have found it simple enough to generate the 4 images randomly. The problem I am having is that I don't want the same image to appear more than once, and am having trouble doing this.

 

As I said, I am new to PHP so I have a lot to learn, but I have built a (fairly basic, admitedly) application in AppleScript on a mac, so I am familiar with many of the basic concepts behind programming, and I could do what I want in AppleScript, but am having trouble translating it to PHP, plus it's been a while since I did that anyway.

 

I'm sure there must be a simple answer, but I can't find it so any suggestions would be greatly appreciated.

 

Here is the script I have so far: (any tips to clean it up also welcome!)

<?php 
		$file_array = array
		(
			'93.jpg', 
			'96.jpg',
			'900.jpg',
			'962.jpg',
			'93.jpg',
			'93.jpg',
			'aerobadge.jpg',
			'aeroX1.jpg',
			'aeroX2.jpg',
			'aeroX3.jpg', 
		); 

		$total = count($file_array); 

		$random = (mt_rand()%$total); 
		$file1 = "$file_array[$random]";
		$file_array= array_values($file_array);

		$random = (mt_rand()%$total); 
		$file2 = "$file_array[$random]";
		$file_array= array_values($file_array);

		$random = (mt_rand()%$total); 
		$file3 = "$file_array[$random]";
		$file_array= array_values($file_array);

		$random = (mt_rand()%$total); 
		$file4 = "$file_array[$random]";

 

Now I tried to get what I want using if statements or repeat loops but one got very confusing and the other froze up the CPU and didn't really work anyway!

 

 

then I figured if I move through the images one by one, removing it from the array as it is used, that would solve it, but I couldn't get it to work....

 

 

any help appreciated,

 

arumdevil

 

Link to comment
Share on other sites

why don't you do something like this:

<?php
        $img_array = array('img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg', 'img5.jpg', 'img6.jpg', 'img7.jpg');
        shuffle($img_array);

        for($i = 0; $i < 4; $i++){
                echo $img_array[$i] ."<br />\n";
        }

        if(you want to display more){
                for($j = $i; $j < $displayNum; $j++){
                        echo $img_array[$j] ."<br />\n";
                }
        }
?>

Link to comment
Share on other sites

Another way to do this is something like this:

<?php 
$file_array = array
(
	'93.jpg', 
	'96.jpg',
	'900.jpg',
	'962.jpg',
	'93.jpg',
	'93.jpg',
	'aerobadge.jpg',
	'aeroX1.jpg',
	'aeroX2.jpg',
	'aeroX3.jpg', 
); 
$rand_array = array();
$tst = $file_array[array_rand($file_array)];
for ($i=0;$i<4;$i++) {
	while (in_array($tst,$rand_array))
		$tst = $file_array[array_rand($file_array)];
	$rand_array[] = $tst;
}
echo '<pre>' . print_r($rand_array,true) . '</pre>'; // debug statement to see what's in the array
?>

 

Ken

Link to comment
Share on other sites

thanks guys, both look like more efficient ways around the problem. As it turns out I have managed to get it working in the meantime, though not as efficiently as either of your suggestions. There were two problems with my original script, 1 being that the array had doubled up values in it which meant for a while while it was in fact working I thought it wasn't  :-[ very emarrasing...

 

anyway, I will play around with both of your suggestions to understand why and how they work.

 

thanks again.

 

humbly, arumdevil

Link to comment
Share on other sites

awesome, thanks guys, I went for Ken's example in the end as I could make sense of it more easily and got it to show more images....but thank you both.

 

now to make it perfect, how can I get it to set the array to every image inside a particular folder? any thoughts on that?

 

thanks

 

arumdevil

Link to comment
Share on other sites

sorry I wasn't clear. In the example above I have explicitly set each item (key??) of the array in the script. what I would like to do instead, is have the script dynamically set each item of the array to each image file inside a particular server on the folder, the purpose being that the user can then change the available images by simply adding them to and deleting them from the folder - with no need to edit the code...

 

hope that makes sense, sorry I've not learnt the proper terminology yet :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.