Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-26-2011, 03:36 AM   PM User | #1
jewradly
New to the CF scene

 
Join Date: Sep 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
jewradly is an unknown quantity at this point
Smile Random File Generator

Yeah hey guys I am new here and fairly new to php. I was wondering if someone could give me a hand to write a code. Basically what I want to be able to do is when the website user clicks on a link, it generates a random image on a separate page. This random image would just be pulled from a folder with a bunch of images. Any help would be great.

Thanks! Austin
jewradly is offline   Reply With Quote
Old 09-26-2011, 04:04 AM   PM User | #2
ASTP001
New Coder

 
Join Date: Aug 2011
Posts: 51
Thanks: 6
Thanked 12 Times in 12 Posts
ASTP001 is an unknown quantity at this point
you can get an array of all the images in that folder by calling the function scandir and then count the number of arrays in it and call rand function to make it randomly pick...

PHP Code:
$images scandir('/images');
$img_num count($images);
$rand rand(2,$img_num); //min is 2 because scandir includes current directory and the directory above
echo "<img src={$images[$rand]}>"
ASTP001 is offline   Reply With Quote
Users who have thanked ASTP001 for this post:
jewradly (09-26-2011)
Old 09-26-2011, 05:31 PM   PM User | #3
jewradly
New to the CF scene

 
Join Date: Sep 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
jewradly is an unknown quantity at this point
thanks a lot. I just have one problem.
When I click the link to generate the random image, it works but it shows the image as "broken" everytime. When i copy paste the image url into a new tab, the url shows it pulling from the main directory instead of the folder where I generated it from. Any ideas?
jewradly is offline   Reply With Quote
Old 09-26-2011, 05:33 PM   PM User | #4
jewradly
New to the CF scene

 
Join Date: Sep 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
jewradly is an unknown quantity at this point
OH and I know it has something to do with the

<img src={$images[$rand]}> part. I just don't know what to change it to.
jewradly is offline   Reply With Quote
Reply

Bookmarks

Tags
code, download, link, php, random

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:13 AM.


Advertisement
Log in to turn off these ads.