| | |
displaying random images
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Feb 2008
Posts: 28
Reputation:
Solved Threads: 0
I have this code which display random images from any folder on a webserver. You just have to give each picture the same name but with a number at the end of it (make foo.bar foo1.bar footype.bar foo2.bar).
Does anybody have any idea how to make this work accept not have any numbers or have the same name on the pictues. I just want it to display any picture in that folder in the webserver regardless of the name. Here is the code.
Does anybody have any idea how to make this work accept not have any numbers or have the same name on the pictues. I just want it to display any picture in that folder in the webserver regardless of the name. Here is the code.
PHP Syntax (Toggle Plain Text)
<?php function image() { $min = 1; //The first number one of your images has $max = 3; //The highest number you image has $number = rand($min, $max); $url = "http://website.com/~ub/_ubr/wp-content/themes/ubcmi/images/icons/image" . $number . ".jpg"; //change the url to your url //for the image split the url into two parts by separating in between the image name and //extension echo "<img src='$url'>"; } ?>
Last edited by alexgv14; Mar 1st, 2009 at 4:08 pm.
ok, I suggest doing something like this:
This means that all is needed is just a number for your images rather then having to name them all.
PHP Syntax (Toggle Plain Text)
<?php function image() { $min = 1; //The first number one of your images has $max = 3; //The highest number you image has $number = rand($min, $max); $url = "http://website.com/~ub/_ubr/wp-content/themes/ubcmi/images/icons/" . $number . ".jpg"; //change the url to your url //for the image split the url into two parts by separating in between the image name and //extension echo "<img src='$url'>"; } ?>
This means that all is needed is just a number for your images rather then having to name them all.
well the whole idea of the script that you have is that it generates the random image through a random number generator in php based on the numbers used to name your images. Therefore using your script you must have numbered images. This is pretty much the easiest way in generating your random images.
If you do not understand the script you are using i suggest going to www.tizag.com and reading up on some php.
If you do not understand the script you are using i suggest going to www.tizag.com and reading up on some php.
You could also just store the image names in an array and refer to the array with the number. An example is the following.
Very basic and hope it does the job.
php Syntax (Toggle Plain Text)
<?php function image() { $min = 1; //The first number one of your images has $max = 3; //The highest number you image has $num = rand($min, $max); //set picture names $pic[1]='firstpic.jpg'; $pic[2]='secondpic.jpg'; $pic[3]='another_name.jpg'; //end of setting picture names $url = "http://website.com/~ub/_ubr/wp-content/themes/ubcmi/images/icons/" .$pic[$num]."; //change the url to your url //for the image split the url into two parts by separating in between the image name and //extension echo "<img src='$url'>"; } ?>
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
What I posted was just what the user wanted. However, there are 2 alternative options. There is the option of php automatically detecting the pictures in the folder and assign them to an array (requires a bit of cpu) or another option is to fetch the arrays from a mysql database.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
![]() |
Similar Threads
- display random images from folder (PHP)
- adding random number (PHP)
- Php random Slide showw (PHP)
- Script only displaying last entries made (Perl)
- Help needed working with prototype.js and php (JavaScript / DHTML / AJAX)
- passing values and displaying images in a GUI (Java)
- 2 Second Delay - One for the Masters... (Windows NT / 2000 / XP)
- Re: Starting Python (Python)
- I lack focus... (Java)
Other Threads in the PHP Forum
- Previous Thread: Problem in uploading images
- Next Thread: insert csv file into mysql through php
| Thread Tools | Search this Thread |
ajax apache api array beginner binary body broken cakephp checkbox class cms code cookies cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert interactive ip javascript job joomla js limit link login mail mediawiki menu mlm mobile msqli_multi_query multiple mycodeisbad mysql navigation oop outofmemmory paging parse paypal pdf php problem procedure query radio ram random recursion regex remote script search server sessions sms source space sql stored subdomain syntax system table tutorial unicode update upload url validator variable video web webapplications websitecontactform xml youtube






