Jump to content

how to show random pictures using php?

Swiebertjeee
Go to solution Solved by mantylt,

Create new array $used_values and every time you select new value ($random) check if $random doesn't already exists inside $used_values. If it does, pick new $random and continue when it doesn't.

I want to show 4 random pictures from a folder (there are a total of 7 pictures) but I'm not sure how to do this. 

 

I know if I have only 1 I can simply use this code : 

<?php 	$total = "10"; 	$file_type = ".jpg"; 	$image_folder = "img/random"; 	$start = "1"; 	$random = mt_rand($start, $total); 	$image_name = $random . $file_type; 	echo "<img class=\"random\" src=\"$image_folder/$image_name\" alt=\"random\" />"; ?>

or put them into an array and do the same thing. 

 

But in that case I will get the possibility for the same images shown. 

Beneath this mask there is an idea, and ideas are bulletproof.

Link to comment
Share on other sites

Link to post
Share on other sites

Create new array $used_values and every time you select new value ($random) check if $random doesn't already exists inside $used_values. If it does, pick new $random and continue when it doesn't.

I am sorry for my english.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×