Assign a PHP Array to a Javascript Array?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
imroue
New php-forum User
New php-forum User
Posts: 62
Joined: Fri Oct 11, 2002 6:53 am
Contact:

Assign a PHP Array to a Javascript Array?

Postby imroue » Sat Nov 09, 2002 10:55 pm

Is it possible to assign values of a PHP array dynamically to a Javascript Array?
:?: :?: :?:

If so, how?

User avatar
Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Sun Nov 10, 2002 12:46 am

Code: Select all

<?

$1img="1.gif";
$2img="2.gif";




echo "<script language=\"JavaScript\">
imgName = new Array()
imgName[0] = \"$1img\"
imgName[1] = \"$2img\"
</script>
";
?>

CFDev
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Nov 13, 2002 9:53 am
Contact:

Postby CFDev » Wed Nov 13, 2002 10:34 am

Here's an example from ActivEdit, where we first check for an attribute, then instantiate a JavaScript array and load the array with values from the PHP variable (attribute).

Code: Select all

<?php

//--- Set Quick Format options.
   
   if (!isset($this->attributes["quickformatnames"])) {
      $this->attributes["quickformatnames"] = array("Normal","Formatted","Address","Heading 1","Heading 2",
      "Heading 3","Heading 4","Heading 5","Heading 6","Numbered List","Bulleted List",
      "Directory List","Menu List","Definition Term","Definition","Paragraph");
   } else {
      $this->attributes["quickformatnames"] = explode(",", $this->attributes["quickformatnames"]);
   }

?>

<script language="JavaScript">

var quickformatNameArray = new Array();

while (list ($key, $val) = each ($this->attributes["quickformatnames"])) {
        $generatedContent .= "quickformatNameArray[$key] = '$val';";
     }

</script>


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 3 guests

cron