PHP echo to HTML help needed

I am setting up a web page with a image slide show. The image layers for the slide show are generated via PHP. That code has worked good but when simplifying it to have variables computed by PHP to be picked up by JavaScript I ran into some echo statement with an error that I cannot resolve. I need some help.

[php]<?php
// Each image line lists:
// image set name, image src address, image display width
$sets = array(
array(
array(“Eyerly”,"_Images/Page01/2012-06-10_Eyerly01.jpg",“400”),
array(“Eyerly”,"_Images/Page01/2012-06-10_Eyerly02.jpg",“400”),
array(“Eyerly”,"_Images/Page01/2012-06-10_Eyerly03.jpg",“400”),
array(“Eyerly”,"_Images/Page01/2012-06-10_Eyerly04.jpg",“400”),
),
array(
array(“Arek”,"_Images/Page01/2012-06-10_Arek01.jpg",“300”),
array(“Arek”,"_Images/Page01/2012-06-10_Arek02.jpg",“300”),
array(“Arek”,"_Images/Page01/2012-06-10_Arek03.jpg",“300”),
array(“Arek”,"_Images/Page01/2012-06-10_Arek04.jpg",“300”),
array(“Arek”,"_Images/Page01/2012-06-10_Arek05.jpg",“300”),
array(“Arek”,"_Images/Page01/2012-06-10_Arek06.jpg",“300”),
array(“Arek”,"_Images/Page01/2012-06-10_Arek07.jpg",“300”),
array(“Arek”,"_Images/Page01/2012-06-10_Arek08.jpg",“300”),
array(“Arek”,"_Images/Page01/2012-06-10_Arek09.jpg",“300”),
array(“Arek”,"_Images/Page01/2012-06-10_Arek10.jpg",“300”),
array(“Arek”,"_Images/Page01/2012-06-10_Arek11.jpg",“300”),
),
array(
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer01.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer02.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer03.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer04.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer05.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer06.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer07.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer08.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer09.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer10.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer11.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer12.jpg",“300”),
array(“Hammer”,"_Images/Page01/2012-06-10_Hammer13.jpg",“300”),
),
array(
array(“Raven”,"_Images/Page01/2012-06-10_Raven01.jpg",“300”),
array(“Raven”,"_Images/Page01/2012-06-10_Raven02.jpg",“300”),
array(“Raven”,"_Images/Page01/2012-06-10_Raven03.jpg",“300”),
array(“Raven”,"_Images/Page01/2012-06-10_Raven04.jpg",“300”),
array(“Raven”,"_Images/Page01/2012-06-10_Raven05.jpg",“300”),
array(“Raven”,"_Images/Page01/2012-06-10_Raven06.jpg",“300”),
array(“Raven”,"_Images/Page01/2012-06-10_Raven07.jpg",“300”),
),
array(
array(“Lux”,"_Images/Page01/2012-06-10_Lux01.jpg",“300”),
array(“Lux”,"_Images/Page01/2012-06-10_Lux02.jpg",“300”),
array(“Lux”,"_Images/Page01/2012-06-10_Lux03.jpg",“300”),
array(“Lux”,"_Images/Page01/2012-06-10_Lux04.jpg",“300”),
array(“Lux”,"_Images/Page01/2012-06-10_Lux05.jpg",“300”),
array(“Lux”,"_Images/Page01/2012-06-10_Lux06.jpg",“300”),
array(“Lux”,"_Images/Page01/2012-06-10_Lux07.jpg",“300”),
array(“Lux”,"_Images/Page01/2012-06-10_Lux08.jpg",“300”),
array(“Lux”,"_Images/Page01/2012-06-10_Lux09.jpg",“300”),
array(“Lux”,"_Images/Page01/2012-06-10_Lux10.jpg",“300”),
array(“Lux”,"_Images/Page01/2012-06-10_Lux11.jpg",“300”),
)
);
// Lets construct the HTML to create a display layer (

) for each image
$setMax = count($sets); // How many image sets do we have, save for JavaScript to pick up
foreach ( $sets as $set )
{
// How many images do we have in the current set, save into $imgMax array for JavaScript to pick up
$imgMax[current($sets)] = count($set);
foreach ( $set as $image )
{
// Save the imeage set name for Javascript to pick up
$setName[current($sets)] = $image[0];
echo "




";
}
}
?>[/php]

I also need help passing signle variables and array variables to JavaScript. I have tried echo’ing the values in an onload without success (the variable are always blank, althous they are good when echo’ing in PHP code).

Thanks in advance for any help you can provide.

I don’t see any JavaScript code here? If you want to pass a variable or array to JS you could possibly json_encode() the data and echo it in your JS. Then you have a JS object to work with.

The main issue I need to resolve first is the syntax error on the last echo statement. I rephrased it many times without success. I suspect a glaring simple thing but I do not see the problem. Please help.

Your quotes are wrong. If you start a string with double quotes you must use double quotes to exit the string. For example:

[php]
$quote = “quotes”;
echo "This string has double " . $quote . “.”;
[/php]

You could also avoid exiting the string if you wish. This is just a preference thing. For example:

[php]
$quote = “quotes”;
echo “This string has double {$quote}.”;
[/php]

Then, if you have double quotes inside of double quotes you need to escape them. For example:

[php]
echo “This string has “double quotes” inside of double quotes.”;
[/php]

Thanks m@tt. I have figured out the echo as:

[php]echo "





";[/php]

The problem moved to the
$imgMax[current($sets)] = count($set); and
$setName[current($sets)] = $image[0]; statements.
See code in my initial post. Both generate an error “Illegal Offset”.

I don’t understand how the value of the current index to an array can be an invalid offset to other arrays that are being created ($imgMax and $setName).

Resolved this: current($array) gives value not current index value.

Is there a way to retrieve the current array index value?
Right now implementing counters in the foreach loop.

Exactly. You can get the key using foreach. For example:

[php]foreach($sets as $key => $set)[/php]

Now $key is the array index.

Thanks so much m@tt. It is appreciated.

Using the $key works and it is more elegant.
I see you answering lots of questions;
lot of people must love having you on this forum.

Now I am going to try passing the PHP var to JavaScript.
If I get to witsend again, I may ask for help again.

As long as you try I am willing to help :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service