RE: [PHP] Creating a javascript array from database data

From: Date: Wed, 15 Aug 2001 16:21:34 +0000
Subject: RE: [PHP] Creating a javascript array from database data
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-62865@lists.php.net to get a copy of this message
-- while you are looping through the date jump in and out of php like this to populate the javascript array <script language="Javascript"> function DataArray() { a = new Array() <? while ($i < $number) { $text = mysql_result($result, $i, "name"); ?> a[<?=$i?>] = <?=$text?> <? $i++; } ?> } HTH Jon Jon Farmer Systems Programmer, Entanet www.enta.net Tel 01952 428969 Mob 07968 524175 PGP Key available, send blank email to pgpkey@bctech.co.uk -----Original Message----- From: Neil Freeman [mailto:neil.freeman@asa.co.uk] Sent: 15 August 2001 17:16 To: PHP General Subject: [PHP] Creating a javascript array from database data Hi there, Well after a few hours roaming around various websites I am at a loss. Here is what I am trying to do: 1) Access a MySQL database which contains 1 table 2) Read the records from this table 3) Store the values returned from this table into javascript array elements, ie, if I get the values "dog", "cat" and "cow" back I want these stored in an array as such: myArray[0] = valueReturned1 myArray[1] = valueReturned2 myArray[2] = valueReturned3 You get the idea. Problem being that I cannot work out how to implement the javascript section of this. At the moment my php script writes the values returned from the database to screen but I require these to be stored in a javascript array. Please can someone help me before I go mad :) Here is my current .php script: ---------------------------------------------- <html> <head> <title>Menus test</title> </head> <body bgcolor="white"> <?php $dbhost = 'localhost'; $dbuser = 'guest'; $dbpass = 'guest'; $dbname = 'IFE'; $dbtable = 'menus'; //------ DATABASE CONNECTION --------// mysql_connect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to database"); mysql_select_db($dbname) or die ("Unable to select database"); $sql = "SELECT * FROM $dbtable"; $result = mysql_query($sql); $number = mysql_numrows($result); $i = 0; if ($number == 0) print "Error - No records found"; elseif ($number > 0) { while ($i < $number) { $text = mysql_result($result, $i, "name"); print "$text"; $i++; } } mysql_free_result($result); mysql_close(); ?> </body> </html> ---------------------------------------------- Thanks, Neil -------------------------------- Email: neil.freeman@asa.co.uk neil_freeman@yahoo.com -------------------------------- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#62865) next »