Topic: What is this code and this operator? (Read 1230 times)
I am a fanatic. So?
Gender:
Posts: 584
3633 credits Members referred : 2
« on: Aug 29, 2006, 09:15:41 PM »
Hi All,
Found this code and it does exactly what I want, but I need to execute multiple times on the same page. The idea is to have a group of image files in a directory and use this code to randomly display one. Since I use it up to three times on the same page and it appears to use the time as the seed for the calculation, without modification it shows the same image 3 times.
I added something and it is working now, but would like to understand it better. My solution was to change a line of code and save the file with a new name. I have 3 files and vary which one is called.
using 0.5 for one file and 1 for another. When I tried 2 it didn't work - which makes me think this sets $imageNumber to a decimal less than 1 but more than 0?
My question is what does the % operator do?
If I understood this code better, maybe there would be a way to only need one file instead of 3.
Global Moderator
Internet Junkie
Gender:
Posts: 1807
9006 credits Members referred : 6
« Reply #4 on: Aug 29, 2006, 10:02:20 PM »
here's some code I'm using to display random images:
Code:
<?php $DIRNAME = "./images/randoms/";// Directory that contains the images
function DisplayRandomImage() { global $DIRNAME; $msg = "";// IMG tag to be returned $y = 0;// counter for the images $images = array(); // array to hold the list of images to display randomly $file_handle = "";
if (is_dir($DIRNAME)) { $file_handle=opendir($DIRNAME); $y = 0; while ($file = readdir($file_handle)) // while there's files to read.... { if (!is_dir($DIRNAME . $file)) { $pieces = explode (".", $file); if ($pieces[0] && $pieces[1]) // filename.ext { if (($pieces[1]=="gif")) // add only .gif files { $images[$y] = $DIRNAME . $file; $y += 1; } } } } closedir($file_handle); if (count($images) > 0) { srand((double)microtime()*1000000); $y = rand(0,(count($images) - 1)); $msg = "<IMG SRC=\"" . $images[$y] . "\" width=\"30\" height=\"80\" alt=\"\">"; } else { $msg = "No image found in \"" . $DIRNAME . "\"\n"; } } else { $msg = "\"" . $DIRNAME . "\" is not a valid directory!\n"; } return $msg; } ?>
This code is in the top of my page, after that HTML is used to create the page and where I want to display a random image I use
I am a fanatic. So?
Gender:
Posts: 584
3633 credits Members referred : 2
« Reply #5 on: Aug 29, 2006, 10:19:18 PM »
Does that code require building a file list? Many of the others I looked at do.
I can call the routine I use multiple times, unfortunately it is displaying the same image - I would guess the time only changes by seconds and therefore the resulting value is the same.
Do you use this with more than one image on the page?
Global Moderator
Internet Junkie
Gender:
Posts: 1807
9006 credits Members referred : 6
« Reply #6 on: Aug 29, 2006, 10:46:56 PM »
Code:
$DIRNAME = "./images/randoms/";
refers to the directory the script looks at (in this case mydomain.com/images/randoms/) The scrip retrieves all the .gif images and displays a random one. If you have other images besides .gif you can change:
Code:
if (($pieces[1]=="gif"))
to:
Code:
if (($pieces[1]=="gif") OR ($pieces[1]=="jpg"))
for instance... hope this helps!
edit: yes I use it with more then one image here: http://www.letsworkonline.com/ (its for the 2 images on the left next to the google ads) it can sometimes display the same image twice, but I guess that chance lowers when you add more images to your random image folder...
« Last Edit: Aug 29, 2006, 10:55:14 PM by Mind_nl »
Global Moderator
Internet Junkie
Gender:
Posts: 1807
9006 credits Members referred : 6
« Reply #11 on: Aug 30, 2006, 12:12:09 AM »
No I think this is exactly what he wants (from what I read in the first post) one random image displayed, but the script called more then one time so he gets random images in different places on his page, thats just how I use this snippet
Code:
<?=DisplayRandomImage();?>
in different places in my HTML
edit: I like your optimization, but I often keep my photoshop files in the same directory (on my PC) and they sometimes get uploaded to the site too, I don't think displaying a .PSD file will work this way, so I'll leave in the part you optimized out
« Last Edit: Aug 30, 2006, 12:15:39 AM by Mind_nl »
No I think this is exactly what he wants (from what I read in the first post) one random image displayed, but the script called more then one time so he gets random images in different places on his page, thats just how I use this snippet
Code:
<?=DisplayRandomImage();?>
in different places in my HTML
Correct, except than the he
BTW I think this is a nice idea for the code gallery. I will make a snippet for random images tomorrow and post it there
The only bad thing about this snippet is that when you use it for more than one images, it can show the same image twice or more times....
Global Moderator
Internet Junkie
Gender:
Posts: 1807
9006 credits Members referred : 6
« Reply #17 on: Aug 31, 2006, 02:08:27 PM »
Good work Nick! I'll be replacing my current random image script with this version. One thing though: the code for displaying one random image is the same as for displaying more then one, looks like the second code line is missing something. Should that be:
I am a fanatic. So?
Gender:
Posts: 584
3633 credits Members referred : 2
« Reply #19 on: Sep 01, 2006, 03:40:50 AM »
Nice job Nik.
I do have a couple of questions.
I would use the code multiple times in the same page, but not with the same function call, will that still keep the same image from appearing?
I will be using it with the script for my directory. Can this be easily saved as a separate file and then called from the template file?
I opted not to take the php class at the local community college, with the directory, I'm now sorry I didn't. I could probably learn it from a book, but the structure of a class makes it easier.