# PHP echo to HTML help needed

**URL:** <https://www.phphelp.com/t/php-echo-to-html-help-needed/18949>\
**Category:** General PHP Help\
**Created:** [November 20, 2012, 3:23am UTC](https://www.phphelp.com/t/php-echo-to-html-help-needed/18949 "2012-11-20T03:23:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arthur](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@Arthur](https://www.phphelp.com/u/Arthur)\
**Post date:** [November 20, 2012, 3:23am UTC](https://www.phphelp.com/t/php-echo-to-html-help-needed/18949/1 "2012-11-20T03:23:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![m11](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@m11](https://www.phphelp.com/u/m11)\
**Post date:** [November 20, 2012, 7:45pm UTC](https://www.phphelp.com/t/php-echo-to-html-help-needed/18949/2 "2012-11-20T19:45:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Arthur](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@Arthur](https://www.phphelp.com/u/Arthur)\
**Post date:** [November 20, 2012, 7:54pm UTC](https://www.phphelp.com/t/php-echo-to-html-help-needed/18949/3 "2012-11-20T19:54:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![m11](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@m11](https://www.phphelp.com/u/m11)\
**Post date:** [November 20, 2012, 7:58pm UTC](https://www.phphelp.com/t/php-echo-to-html-help-needed/18949/4 "2012-11-20T19:58:20Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Arthur](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@Arthur](https://www.phphelp.com/u/Arthur)\
**Post date:** [November 20, 2012, 9:42pm UTC](https://www.phphelp.com/t/php-echo-to-html-help-needed/18949/5 "2012-11-20T21:42:34Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Arthur](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@Arthur](https://www.phphelp.com/u/Arthur)\
**Post date:** [November 21, 2012, 12:11am UTC](https://www.phphelp.com/t/php-echo-to-html-help-needed/18949/6 "2012-11-21T00:11:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![m11](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@m11](https://www.phphelp.com/u/m11)\
**Post date:** [November 21, 2012, 12:22am UTC](https://www.phphelp.com/t/php-echo-to-html-help-needed/18949/7 "2012-11-21T00:22:18Z")

</div>

> [@Arthur](#):
>
> Resolved this: current($array) gives value not current index value.

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

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

Now $key is the array index.

---

<div class="post-metadata">

**Author:** ![Arthur](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@Arthur](https://www.phphelp.com/u/Arthur)\
**Post date:** [November 21, 2012, 2:31am UTC](https://www.phphelp.com/t/php-echo-to-html-help-needed/18949/8 "2012-11-21T02:31:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![m11](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@m11](https://www.phphelp.com/u/m11)\
**Post date:** [November 21, 2012, 3:27am UTC](https://www.phphelp.com/t/php-echo-to-html-help-needed/18949/9 "2012-11-21T03:27:08Z")

</div>

As long as you try I am willing to help 🙂
