Dynadot .com transfer sale, only $10.12 with coupon code DYNA12, available while supplies last, ends October 1, 2026.

Outputting Javascript array values

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

Rudy

Established Member
Impact
16
Hey guys,
There's a little PHP here, but I think this is a javascript issue... I am creating a javascript array, and within a php while() loop, I am adding values to the array and then am trying to output the results. However, what I want outputted to the browser is not being sent.

I am trying to output an image, but it is not happening.

Any ideas why this is happening?

Code:
<?PHP
/*
Author: David White
Date Started: 02/06/2007
Description: 	This script will allow a user to browse through pictures
		in a directory to select one picture. It will place all of the contents
		in a directory into an array which will be displayed one-by-one on the click
		of a button.
*/

// Set the Directory Location
$directory = opendir('../premier_images/');

// Put every picture in the directory into an array
echo "<script type=\"text/javascript\">
		var output = new Array()
		var i = 0;
	";
while ($picture = readdir($directory)) {
	echo "	output[i] = " . $picture . "
		document.write('
			<div id='+output[i]+' style=\'<!-- display:none;-->align=center;valign=center;\'>
				<img src=\'../premier_images/'+output[i]+\'>
				<br />
			</div>
			');
		i++;
		document.write(i);
		";
}
// ignore this next line... it has a php function that I still need to switch over to a javascript function.... it's not causing any problems
echo "</script><br /><br /><input type='button' value='Next Photo' onClick=\"next_photo_gallery('".current($output)."', '".next($output)."')\">";
?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
erm... I think the problem is that your while loop is in php, but you are increasing var i in javascript lol.

Lets see what I can do:
Code:
<?PHP
/*
Author: David White
Date Started: 02/06/2007
Description: 	This script will allow a user to browse through pictures
		in a directory to select one picture. It will place all of the contents
		in a directory into an array which will be displayed one-by-one on the click
		of a button.
*/

// Set the Directory Location
$directory = opendir('../premier_images/');

// Put every picture in the directory into an array
echo "<script type=\"text/javascript\">
		var output = new Array()
	";
$i = 0;
while ($picture = readdir($directory)) {
	echo "	output[".$i."] = " . $picture . "
		document.write('
			<div id='+output[".$i."]+' style=\'<!-- display:none;-->align=center;valign=center;\'>
				<img src=\'../premier_images/'+output[".$i."]+\'>
				<br />
			</div>
			');
		document.write(".$i.");
		";
$i++;
}
// ignore this next line... it has a php function that I still need to switch over to a javascript function.... it's not causing any problems
echo "</script><br /><br /><input type='button' value='Next Photo' onClick=\"next_photo_gallery('".current($output)."', '".next($output)."')\">";
?>
try that... I just did the "$i" bit in php instead of javascript...

It all seems a bit ott to be honest though... wouldn't it be much easier to do the whole thing in PHP?
 
0
•••
Olitt — high-converting AI websites, only from $1/moOlitt — high-converting AI websites, only from $1/mo

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back