Forum Moderators: coopster

Message Too Old, No Replies

for and while loops

printing some things...

         

ahmedtheking

6:02 pm on Nov 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ive got a javascript that creates a slideshow of some images that i specify in such way:

fadeimages[0]="photo1.jpg"
fadeimages[1]="photo2.jpg"
fadeimages[2]="photo3.jpg"
...

and so on

I have been trying to make a php script that reads a folder and searches for images. with those images, it uses a loop to print each image in such way so that the javascript will be able to work!

here is my php script! (i have had no luck!)

$dir = "images/random/";

// Open a known directory, and proceed to read its contents
if ($dh = opendir($dir)) { // open the dir
while (($file = readdir($dh))!= false) // read the files
{
$ext = explode('.',$file);
if ($file!= '.' && $file!= '..' && $ext[1]=='jpg' && $ext[1]=='gif' && $ext[1]=='jpeg' && $ext[2]!='LCK') {

$filearray = explode('.',$file);
$filename = reset($filearray);
/* $getcat = urlencode($filename);

for ($n = "0"; $n >= 0; $n++) { */

print ('fadeimages[$n]="/$dir$filename"\n');
/* } */
}
}
closedir($dh);
}

thanks for any help!

jatar_k

6:13 pm on Nov 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I have this in my little snippet lib, seems to work

<? 
$the_array = Array();
$handle = opendir('/root/path/to/dir');
while (false!== ($file = readdir($handle))) {
if ($file!= "." && $file!= ".." &&!is_dir($file)) {
$namearr = split('\.',$file);
if ($namearr[count($namearr)-1] == 'jpg') $the_array[] = $file;
}
}
closedir($handle);
sort ($the_array);
reset ($the_array);
// output
$counter = 0;
while (list ($key, $val) = each ($the_array)) {
echo 'fadeimages[' . $counter . ']="' . $val . '"' . "\n";
$counter++;
}
?>

DaButcher

7:01 pm on Nov 2, 2004 (gmt 0)

10+ Year Member



Why do you "glue" so much together?

echo 'fadeimages[' . $counter . ']="' . $val . '"' . "\n";

Why not:

echo fadeimages[$counter]="{$val}\n";?

I think the second sollution gives less system stress?

ahmedtheking

7:36 pm on Nov 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks you lot! I shall try this out and get back to you! Thank you! :D

jatar_k

7:45 pm on Nov 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> why do you glue so much

only reason was ahmedtheking mentioned needing it to be written to js, not sure why I cat'ed so many times, could be written like so

echo 'fadeimages[',$counter,']="',$val,'"',"\n";

which is much faster as the string doesn't have to be concatenated before it can be displayed.

ahmedtheking

2:23 pm on Nov 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks peeps! i works! :D