PHP and JavaScript Array's

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

PHP and JavaScript Array's

Post by Calimero »

Oki, lets start:

I query the DB and get results:

QUERY:

select field1, field2, field3, field4 from table1 where I_decide_on_the_search_condition :)

then goes the while loop and mysql_fetch_array and

NOW I'M STUCK...


If I would do it just in PHP I would create variables and create a page inside the while loop.


BUT I want to complicate my and othee life's, and I want to transfer all results to JS table to enable client side sorting (script that dinamicaly sorts the contents of the table based on the array data)

Now - How would I transfer the data from php to JS array's

I tried JS in the while loop - but as it is client side - nothing good happened, and the only solution I found was to collect all the data into separate array's (each column is gathered into its own array) and then echo-ed them as a string to JS which then exploded it to single parts and rebuilt the original results by creating multidimensional array.


Is there any SIMPLER way of doing this - and if code is available, I would appreciate it.


Thanks Ahead.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

That's how I would do it:

Code: Select all

<pre><script language="JavaScript" type="text/javascript">
<!--
<?php
$numArray = range(0, 9);
$values = implode(", ", $numArray);
echo "\tnumbers = new Array($values);\n";
echo "\temployees = new Array();\n";
$empArray = array();
$empArray[] = array('John', 'Programmer', 40, 5);
$empArray[] = array('Thomas', 'Analist', 20, 3);
$empArray[] = array('Emma', 'DBA', 40, 6);
$empArray[] = array('Bill', 'Designer', 20, 3);
foreach ($empArray as $employee) {
	$values = "'" . implode("', '", $employee) . "'";
	echo "\temployees.push(new Array($values));\n";
}
?>
	document.writeln(numbers.length + ' Numbers:');
	for (i = 0; i < numbers.length; i++)
		document.writeln(numbers[i]);
	document.writeln();
	document.writeln(employees.length + ' Employees:');
	for (i = 0; i < employees.length; i++) {
		document.writeln('Employee #' + i + ':');
		for (j = 0; j < employees.length; j++)
			document.writeln(employees[i][j]);
		document.writeln('----');
	}
//-->
</script></pre>
Output (HTML):

Code: Select all

&lt;pre&gt;&lt;script language="JavaScript" type="text/javascript"&gt;
&lt;!--
	numbers = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
	employees = new Array();
	employees.push(new Array('John', 'Programmer', '40', '5'));
	employees.push(new Array('Thomas', 'Analist', '20', '3'));
	employees.push(new Array('Emma', 'DBA', '40', '6'));
	employees.push(new Array('Bill', 'Designer', '20', '3'));
	document.writeln(numbers.length + ' Numbers:');
	for (i = 0; i &lt; numbers.length; i++)
		document.writeln(numbers&#1111;i]);
	document.writeln();
	document.writeln(employees.length + ' Employees:');
	for (i = 0; i &lt; employees.length; i++) &#123;
		document.writeln('Employee #' + i + ':');
		for (j = 0; j &lt; employees.length; j++)
			document.writeln(employees&#1111;i]&#1111;j]);
		document.writeln('----');
	&#125;
//--&gt;
&lt;/script&gt;&lt;/pre&gt;
Output (parsed)

Code: Select all

10 Numbers:
0
1
2
3
4
5
6
7
8
9

4 Employees:
Employee #0:
John
Programmer
40
5
----
Employee #1:
Thomas
Analist
20
3
----
Employee #2:
Emma
DBA
40
6
----
Employee #3:
Bill
Designer
20
3
----
Hope it helps!

-- Scorphus
Post Reply