Use PHP Array inline in Javascript Code

Posted on 2009-02-16
Last Modified: 2013-12-12
I'm playing around with building a Jeopardy game using PHP, MySql, Javascript and XHTML and maybe jQuery, unless there is a preexisting configurable web application already available.  I have only just started and I'm interested in being able to access PHP arrays from javascript and/or accessing javascript arrays from within PHP.  I have the attached code, which almost does what I am interested in doing but the snippets that fetches the PHP arrays from within javascript doesn't work.

document.getElementById('categories' + (i+1)).innerHTML = '<? echo $catArray[(jeopardyMultiplier-1)][i] ?>';

I take it the subscripts can't be used in that manner within the php code as they are javascript variables.

Can someone educate me on how to do this or on how to simply convert a PHP array to a JS array, which might be easier.




function Conectarse()


	global $dbusername,$dbpassword,$db;

   if (!($link=mysql_connect("localhost",$dbusername,$dbpassword)))


      echo "Error connecting to the database server.";



   if (!mysql_select_db($db,$link))


      echo "Error connecting to the database on the server.";



   return $link;



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Untitled Document</title>

<style type="text/css">

body, table, table td {






#board {



#board tr td{





border:#000000 solid 3px;








<table id="board">

  <tr id="categories">

    <td id="categories1"></td>

    <td id="categories2"></td>

    <td id="categories3"></td>

    <td id="categories4"></td>

    <td id="categories5"></td>


  <tr id="row1">

    <td id="row1_1"></td>

    <td id="row1_2"></td>

    <td id="row1_3"></td>

    <td id="row1_4"></td>

    <td id="row1_5"></td>


  <tr id="row2">

    <td id="row2_1"></td>

    <td id="row2_2"></td>

    <td id="row2_3"></td>

    <td id="row2_4"></td>

    <td id="row2_5"></td>


  <tr id="row3">

    <td id="row3_1"></td>

    <td id="row3_2"></td>

    <td id="row3_3"></td>

    <td id="row3_4"></td>

    <td id="row3_5"></td>


  <tr id="row4">

    <td id="row4_1"></td>

    <td id="row4_2"></td>

    <td id="row4_3"></td>

    <td id="row4_4"></td>

    <td id="row4_5"></td>


  <tr id="row5">

    <td id="row5_1"></td>

    <td id="row5_2"></td>

    <td id="row5_3"></td>

    <td id="row5_4"></td>

    <td id="row5_5"></td>




$query_categories="select * from categories";


for( $i=0; $temp1=mysql_fetch_array($result_categories); $i++ )


		for( $j=0; $j<5; $j++ )


		$catArray[$i][$j] = $temp1[$j];




<script type="text/javascript">

jeopardyMultiplier = 1;

for (i = 0; i <5; i++) {

document.getElementById('categories' + (i+1)).innerHTML = '<? echo $catArray[(jeopardyMultiplier-1)][i] ?>';


for (j = 1; j <=5; j++) {

for (i = 1; i <=5; i++) {

document.getElementById('row'+j+'_'+i).innerHTML = jeopardyMultiplier * 100 * j;






Open in new window

Question by:sscotti
    LVL 34

    Expert Comment

    by:Terry Woods
    Remember that PHP runs on the server to generate the page that is sent to the browser, whereas javascript runs while the user is viewing the page.
    LVL 34

    Accepted Solution

    At the start of your javascript code, you could use PHP to generate javascript code which sets all the jeopardyMultiplier values in a javascript array. Your javascript code that's output from the PHP would end up looking something like the answer to this question:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    php simulator 5 56
    php session question (Code Igniter) 5 22
    c# string 6 26
    HTML5 and Javascript 3 21
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now