PDA

View Full Version : Storing values into JavaScript from PHP


damojones
04-06-2004, 09:23 PM
Hi

I am new to JavaScript, so sorry if this sounds stupid. :confused:

I am importing data from a MySQL database using PHP. The values are displayed on screen in rows, I am trying to select one of those rows and have the values from all items on that row transfered into JavaScript variables.

I am sure it should be possible, but am unsure of how to get there.

eg.
item 1. Value, Value
item 2. Value, Value
item 3. Value, Value
item 4. Value, Value

These values are generated from a select statement in MySQL.

Once one of these lines have been clicked upon. ie item 1 - 4
Then I would like to set the values from that line (all three of them) to variables for use in comparisons with other values.

Hope someone can help!

Kor
04-07-2004, 08:27 AM
That looks to me as a mainly php problem than a javascript one...

Let's see... you have a MySQL data base. To transform data in javascript variables you have to build a php application which will generate either the whole HTML document, including embedded javascript code lines or an external .JS file

Now... for a futher javascript usage the final way data should be in array mode, I would recomend the double array mode

var item = new Array()
item[0] = new Array('item0','value0','anothervalue0');
item[1] = new Array('item1','value1','anothervalue1');
item[2] = new Array('item2','value2','anothervalue2');
...

Now you can easily use them as, let's say

item[0][0] (which will be the value 'item0 in my example) or
item[2][1] (which will be the value 'value2') and so on

You may use in loops either in for/to statements

damojones
04-07-2004, 10:10 AM
hi

many thanks

However how would I get the values into the array from the database values being displayed?


regards

Kor
04-07-2004, 10:30 AM
However how would I get the values into the array from the database values being displayed?


As I said... You need a PHP application to do that (to get the values from MySQL data base and to generate a HTML (or JS) file with all those data written as javascript variables...

Garadon
04-07-2004, 10:41 AM
This is just an example

$result=mysql_query('SOME SELECT');
$jsArray="var item = new Array();";
for($I=0;$I<mysql_num_rows($result);$I++)
{
list($item,$value,$anothervalue)=mysql_fetch_row($result);
$jsArray.="item[".$I."]=new Array('".$item."','".$value."','".$anothervalue."');";
}

print($jsArray):