PDA

View Full Version : How do I add the following 'Display Random Image' Code?


baptizer
07-13-2004, 11:13 AM
Hello All!
I am hoping someone can help me. I know there are a few other 'display random image' codes floating around, however, I would like to get this one to work. The difference between this code and the other code floating around? Well, this code does not require you to number your images 1,2,3,4,5,etc. It adds the images to an array regardless of their name.

I have this code working on my HTML frontpage (not related to vb at all) and I would like to get it to work with my vbadvance homepage. Basically I am trying to make www.2darkpark.com/vbulletin/2dp.php look like www.2darkpark.com (the random display imager is in the top right corner). Can anyone help me with this code? I dont know which template to add it too! here is the code:
<?php
$DIRNAME = "./images/"; #The path RELATIVE to this script to the dir containing the images.
#The value should end with a "/" character!
#This directory should contain ONLY images.

######## NOTHING BELOW THIS LINE SHOULD NEED TO BE CONFIGURED!!! ########
$images = array(); #Holds the list of images to choose randomly from.
$file_handle=opendir($DIRNAME);
$x = 0;
while ($file = readdir($file_handle)) {
if($file == '.' || $file == '..') { continue; }
if(is_dir($DIRNAME.$file)) { continue; }
$images[$x] = $DIRNAME . $file;
$x += 1;
}
closedir($file_handle);

function DisplayRandomImage() {
global $images;
if (count($images) > 0) {
srand((double)microtime()*1000000);
$rnd_img = rand(0,(count($images) - 1));
return $rnd_img;
}
}
?>


This is the call that I use in my HTML website front page:
<table width="175" border="0" cellspacing="0" cellpadding="0" height="67">
<tr>
<td width="175">
<div align="center"><a href="http://www.2darkpark.com/"><IMG SRC="<?=$images[DisplayRandomImage()];?>" ALT="" border="0"></a></div>
</td>
</tr>
</table>

It works perfectly on the HTML page, just cant get it to work on the vbadvanced page :(

Brian
07-13-2004, 01:41 PM
What exactly is it doing /not doing? The only obvious thing I see is that you need to remove the <? and ?> tags inside the image tag.

Our Sponsors
 

baptizer
07-13-2004, 04:36 PM
I tried that, and it gave me an error :( It just isnt displaying the image, and it is displaying an error at the top of the page. I guess my main problem is I dont know where to enter the php code...which template (and where inside that template?)

I guess I could rename all my images to 1.jpg, 2.jpg, etc and use a simple random imager....but this is much more powerful.

Brian
07-13-2004, 04:39 PM
What error is it giving you? If you haven't already, you need to add the module through your admin cp and make sure you are including the file you posted above.

Our Sponsors
 

baptizer
07-14-2004, 03:34 PM
Hello Brian:
Maybe that was my problem: I was actually putting the php code directly into the header (then tried headinclude, then tried phpinclude). I dont remember the exact error, but Ill install the code again and get back to you.

Exactly how do I include a file? For instance, if i put the php code into its own file, how do I call the file in the template?

Brian
07-14-2004, 03:37 PM
When you add a new module through the admin cp, there is an option there to select a file to include.