#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    2
    Rep Power
    0

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Location
    UK
    Posts
    527
    Rep Power
    14
    just print out the javascript with the php variables as the values. Something like this -may have errors but it gives the general idea.
    PHP Code:
    <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

    echo
    "<script language='javascript'>\n";
    echo
    "myarray = new Array($number-1);\n";
    while (
    $i $number

    $text mysql_result($result$i"name"); 
    print 
    "$text"
    echo
    "myarray[$i]=$text;";
    $i++; 


    echo
    "\n</script>";
    mysql_free_result($result); 
    mysql_close(); 
    ?> 

    </body> 
    </html>
    Doc MacGurgles Babylonian Lizard Tooth Oil worked for me
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    2
    Rep Power
    0
    OK thanks a lot for your help JohnB. That appears to get the desired result

IMN logo majestic logo threadwatch logo seochat tools logo