View Full Version : Help:Changing img src:PHP and JavaScript

10-11-2012, 07:11 AM
I've modified a snippet of code from this very site that builds an image array from a folder and then rotates through the images in sequence. The modification i made values a random <img id=x> to a random entry in the image array of 19 items.
partA: the html.
I've never posted here before so i hope i posted this right w the tags.

I know partB works.
PartA shows no image change when run. I am not sure how to step through this in firebug but there appears to be no js errors.
Any thoughts would be greatly appreciated.
Kindest regards,

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="getimages.php"></script>
<script type="text/javascript">
function rotateimages(){
$thetd =rand(1,8);
echo 'document.getElementById("p'.$thetd.'").setAttribute("src", "images/smimages/"+galleryarray['.$theimg.']);'

setInterval("rotateimages()", 2500)
<td><img id="p1" src="images/smimages/Copa3sm.jpg" /></td>
<td><img id="p2" src="images/smimages/TommySm.jpg" /></td>
<td><img id="p3" src="images/smimages/ViSm.jpg" /></td>
<td><img id="p4" src="images/smimages/TrioSm.jpg" /></td>
<td><img id="p5" src="images/smimages/rom2sm.jpg" /></td>
<td><img id="p6" src="images/smimages/sp2sm.jpg" /></td>
<td><img id="p7" src="images/smimages/pr3sm.jpg" /></td>
<td><img id="p8" src="images/smimages/PelSm.jpg" /></td>

the php code:
//PHP SCRIPT: getimages.php
Header("content-type: application/x-javascript");

//This function gets the file names of all images in the current directory
//and ouputs them as a JavaScript array
function returnimages($dirname="images/smimages/") {
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
echo 'galleryarray['.$curimage.']="'.$file .'";';


echo 'var galleryarray=new Array();'; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names

I wanted to thank the original individual for the core piece of this code.
It works great. An they are a good php developer.