Jump to content

getting a php array into javascript - help needed


glennn.php

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++; 
Link to comment
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/
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.