Jump to content

Archived

This topic is now archived and is closed to further replies.

glennn.php

getting a php array into javascript - help needed

Recommended Posts

i need to get an array into this javascipt, and i'm unable - can someone offer a hand?

global $onMouseOver, $onMouseOut, $picsArray, $textArray, $picUrl;                                   
                $idx = 0;
                while ($idx <= 6) {
                        $pic  = $picsArray[$idx];
                        $purl = $picUrl[$pic];

                        $text  = $textArray[$idx];

                       
                ?>
                <script language="JavaScript" type="text/javascript">

                  Text[8]=["<?php print "$text"; ?>","next line..."]
                  applyCssFilter()
                                </script>

<?
       
//////

$line = "<td width=\"100\" background=\"stars/".$pic."\"><a href=\"".$phpLink."\" target=\"_blank\">
<img src=\"stars/gold.gif\" width=\"100\" height=\"100\" border=\"0\" onMouseOver=\"stm(Text[8],Style[5])\" onMouseOut=\"htm()\" /></a></td>";
                        }
                        print $line."\n";
                        $idx++; 

Share this post


Link to post
Share on other sites
Hello Glenn (I think)0,

Although the purpose of PHP is the capability of embedding with HTML (or JS in your case), I like to stick with concatinating the output within PHP variables.  Doing this will fix your problem easily. 

Remember you can still embed the PHP with JS, but eventually I believe you will like this way better. 

One other point of assistance I can help you with is to use single quotes whenever possible.  When your page is interpreted, double quotes are first read to check for embedded items, then read as a string.  Single quotes are read as a literal, and you can just escape and concatenate your items within it like so:

[code=php:0]
<?php
$name = 'Will';
echo 'My name is '.$name.'.  How are you?';
?>
[/code]

Ok.  Let's look at this:
[code=php:0]
<?php
$isx=0;
// Beginning of our JS content
$js = '<script language="javascript" type="text/javascript">';
// Create the "Text" variable array
$js .= 'Text = new Array();';
while ($idx <= 6) {
    $js .= 'Text['.$idx.']='.$textArray[$idx].';';
}
$js .= '</script>';
?>
[/code]

This will help you build your JS array and allow you to access it via the "Text" variable. 

Is this the answer you are looking for?

-- Will http://www.phpfever.com/

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.