I'm not sure if this is the problem but it is an important point. print
"\"$text\""; does not output anything to screen, as with all prints and
echos it outputs it to html (in this case within a js function definition).
Try viewing the source produced.
Tim
----------
From: Neil Freeman [SMTP:[EMAIL PROTECTED]]
Sent: 15 August 2001 18:10
To: Sheridan Saint-Michel
Cc: php-general
Subject: Re: [PHP] Creating a javascript array from database data
Thanks a lot for your help Sheridan,
This now appears to work ok :) but if I remove the:
print "\"$text\"";
line (as it is not required), I receive errors complaining about a
syntax
error (related to what?) and that 'text' is undefined. As this line
simply
outputs the value to screen why should this cause a problem if it is
removed?
Or could it be some other problem?
Neil
PS: Here is my updated 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 "text = new Array(";
while ($i < $number)
{
$text = mysql_result($result, $i, "name");
$i++;
if ($i < $number)
print ",";
else
print ")\n";
}
echo "</Script>\n";
}
mysql_free_result($result);
mysql_close();
?>
</body>
</html>
--------------------------
Sheridan Saint-Michel wrote:
> ***************************************************************
> This message was virus checked with: SAVI 3.48
> last updated 14th August 2001
> ***************************************************************
>
> The thing to ALWAYS remember when working with both PHP and
> JavaScript is that PHP is Server-Side and JavaScript is
Client-Side.
>
> What this mean is practical terms is that when going from
JavaScript
> to PHP you have to submit or redirect back to the server... and
when
> going from PHP to JavaScript (Like you are trying to do here) you
> have to make sure your PHP outputs JavaScript.
>
> So instead of printing $text you need to actually print the
JavaScript...
>
> So in your case change the middle of your script to something
like:
>
> $number = mysql_numrows($result);
>
> $i = 0;
>
> if ($number == 0)
> print "Error - No records found";
> else
> {
> echo "<Script Language=\"JavaScript\">\n";
> echo "text = new Array(";
> while ($i < $number)
> {
> $text = mysql_result($result, $i, "name");
> print "\"$text\"";
> $i++;
> if ($i < $number)
> print ",";
> else
> print ")\n";
> }
> echo "</Script>\n";
> }
>
> }
>
> Note: This is my quick *untested* fix done by modifying
> your code as little as possible. I would suggest you use
> mysql_fetch_row and use your loop to parse it rather than
> making several calls to mysql_result (especially if you are
> ever going to have more than a few entries in the DB).
>
> Sheridan Saint-Michel
> Website Administrator
> FoxJet, an ITW Company
> www.foxjet.com
>
> ----- Original Message -----
> From: Neil Freeman <[EMAIL PROTECTED]>
> To: PHP General <[EMAIL PROTECTED]>
> Sent: Wednesday, August 15, 2001 11:16 AM
> 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';
> >
> > file://------ 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: [EMAIL PROTECTED]
[EMAIL PROTECTED]
--------------------------------
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]