NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Need PHP Help with Rotating Random Images

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 10-31-2006, 04:04 PM THREAD STARTER               #1 (permalink)
New Member
Join Date: Oct 2006
Posts: 12
radiocricket is an unknown quantity at this point
 



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);
????: NamePros.com http://www.namepros.com/programming/252978-need-php-help-rotating-random-images.html
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:
????: NamePros.com http://www.namepros.com/showthread.php?t=252978

"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 by radiocricket; 11-02-2006 at 05:50 AM. Reason: Problem Solved
radiocricket is offline  
Old 11-02-2006, 01:02 AM   #2 (permalink)
NamePros Regular
 
beaver6813's Avatar
Join Date: May 2005
Location: England
Posts: 392
beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough
 




Heh, i know it sounds to simple but have you made sure that you've chmod'd the image directory to something like 777?
beaver6813 is offline  
Old 11-02-2006, 05:29 AM THREAD STARTER               #3 (permalink)
New Member
Join Date: Oct 2006
Posts: 12
radiocricket is an unknown quantity at this point
 



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
radiocricket is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 08:55 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger