View Full Version : Help transfering PHP array to JS

07-27-2009, 07:05 PM
Well i just got booted off the server after writing an essay here so this is the short version

-Ive never worked html php or javascript, so any help, tips, etc is appreciated.
-Im making a database for some1 to store client information on and they want it to be client-side
-Each row represents 1 client
-Made a working php version, now trying to make a working JS version

heres the PHP version that prints the table correctly:
($data = 2D array of all information, $columns = array of column headers, $numRows = number of rows/clients)

function printTable()
echo '<table class = "halloween" border = "1">';
echo '<tr>';
foreach($this->columns as $header => $order)
echo '<th>';
echo $header;
echo '</th>';
for($i = 0; $i < $this->numRows; $i++)
echo '<tr>';
foreach($this->data[$i] as $value)
echo '<td>';
echo $value;
echo '<input type = "text" size = 10>';
echo '</td>';
echo '</tr>';
echo '</table>';

that works!!

now i am trying to pass this information to JS file so it will be client side

I started by trying to pass the array of column headers, heres what i got

function printJSTable()
echo '<script src = "java_script.js">';
echo 'var js_Array = new Array();\n';
foreach($this->columns as $header => $order)
echo 'js_Array.push($header);\n';
echo '</script>';
echo '<input type = "button" value ="click" onclick = drawTable("js_Array")';

and here is what drawTable() currently is

function drawTable(columns)
alert("Hello World");
for(i=0; i<columns.length; i++)

When the button "click" is clicked it alerts "Hello World" and then alerts "j" then "s" then "_"

well it alerts letter by letter the name of the array

any tips? even tips addressing different issues would be greatly appreciated, any help at all really, even big picture stuff


07-27-2009, 08:36 PM
As was pointed out in another post, the actual database is going to be server side, I meant that I want the representation of the database (aka a table) to be client side. I alrdy have a table working in PHP but finding it very difficult to transfer that to JavaScript

rnd me
07-27-2009, 09:19 PM
I alrdy have a table working in PHP but finding it very difficult to transfer that to JavaScript

you must not have heard about php's json_encode()...

it's a one-call conversion from a php array to json, which is actually javascript.
couldn't be any simpler.

it's a whole lot faster and easier than trying to build js from strings in php loops.

Old Pedant
07-27-2009, 09:40 PM
LOL! Oh, he says. So you don't have to write code like I did???

07-27-2009, 11:04 PM
haha guess so. thanks for the help!