Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-11-2012, 06:11 AM   PM User | #1
arpoador
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
arpoador is an unknown quantity at this point
Unhappy Help:Changing img src:PHP and JavaScript


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.
partB:getimages.php.
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,
David

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

window.onload=function(){
setInterval("rotateimages()", 2500)
}
</script>
<table>
<tr>
<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>
</tr>
</table>
</body>
</html>

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();
$curimage=0;
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 .'";';
$curimage++;
}
}

closedir($handle);
}
return($files);
}

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.
cheers
arpoador is offline   Reply With Quote
Reply

Bookmarks

Tags
dynamic, images, javascript, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:34 PM.


Advertisement
Log in to turn off these ads.