NameSilo

Need PHP Help with Rotating Random Images

Spaceship Spaceship
Watch

radiocricket

Established Member
Impact
0
CLOSED- PROBLEM SOLVED- Need PHP Help with Rotating Random Images

This is driving me crazy, I can't get this PHP to work. I purchased a license from www.dyn-web.com to use this PHP and javascript and even they can't see what I'm doing wrong. Here it goes- these are the instructions straight off www.dyn-web.com:



Place script tags for the required JavaScript files in the head of the document:

<script src="js/dw_rotator.js" type="text/javascript"></script>
<script src="js/dw_random.js" type="text/javascript"></script>


A few PHP variables and functions need to be placed or included early in the document:

<?php
$path_to_images = "images/"; // path to your images
$num_to_rotate = 12; // number or "ALL" (to rotate all images in directory)

// returns string to echo into JavaScript images array
function getJSRandomized($path, $list, $num) {
$str = '"';
mt_srand( (double)microtime() * 1000000 );
shuffle($list);
if ( $num == "ALL" ) $num = count($list);
for ($i=0; $i<$num; $i++) {
$str .= $path . $list[$i] . '", "';
}
// remove last comma and space
$str = substr($str, 0, -3);
return $str;
}

function getImagesList($path) {
$ctr = 0;
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// add checks for other image file types here, if you like
if ( preg_match("/(\.gif|\.jpg)$/", $img_file) ) {
$images[$ctr] = $img_file;
$ctr++;
}
}
closedir($img_dir);
return $images;
} else {
return false;
}
}
?>


Include a call to the dw_Rotator.start function in the body onload event handler attribute:

<body onload="dw_Rotator.start()">

Place the following mixture of JavaScript and PHP tag segments where the image rotation is to take place in your document:

<?php if ( $image_list = getImagesList($path_to_images) ) : ?>

<script type="text/javascript">
/*************************************************************************
This code is from Dynamic Web Coding at dyn-web.com
Copyright 2001-5 by Sharon Paine
See Terms of Use at dyn-web.com/bus/terms.html
regarding conditions under which you may use this code.
This notice must be retained in the code as is!

See full source code at dyn-web.com/scripts/rotate-rand/
*************************************************************************/
var imgList = [
<?php echo getJSRandomized($path_to_images, $image_list, $num_to_rotate); ?>
];
var rotator1 = new dw_RandRotator(4000); // rotation speed
// images array, width and height of images, transition filter (boolean)
rotator1.setUpImage(imgList, null, null, false);
</script>

<?php else : ?>
<!-- image to display if directory listing fails -->
<img src="images/smile.gif" width="88" height="88" alt="">

<script type="text/javascript">dw_Rotator.start = function() {};</script>

<?php endif; ?>


I've changed the names to the paths I'm using but still it doesn't work. All I get is the image I want displayed if the directory fails. So I'm assuming my directory is failing. I don't have any .php saved so maybe that's the problem? I have no idea how PHP works, I've learned more code than I ever thought I would but obviously not enough. Dyn-Web did give me this suggestion:

"The most common problem with the PHP version involves the path to the images. If the images folder is in a directory under the document, usually it works with no problem. Otherwise you could include $_SERVER['DOCUMENT_ROOT'] in the path and this generally solves the problem."


I tried this but can't be sure I implemented it correctly. I would really like to pull images out of a directory. I will have hundreds to rotate through and I don't want to have to manually type in each image name into each webpage. I'd much rather have the PHP and just save images into a directory and never worry about updating each page every time I add an image. I love the javascript, it is exactly what I was looking for so I don't want to give up on it.

I am designing with Dreamweaver so maybe that's causing the problem? In one line of the code is says to delete the last comma and space which I have done on my page, it hasn't made a difference if I do it or not. Please help and forgive me if I'm asking anything ignorant.

-Janice
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Heh, i know it sounds to simple but have you made sure that you've chmod'd the image directory to something like 777?
 
0
•••
I don't know what chmod'd stands for!

Someone has helped me with the problem by writing a different script. Thanks for the help!

-Janice
 
0
•••
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back