Think in terms of output...  The JavaScript is literally going to
try and run the output of the PHP (It doesn't even know PHP
exists).  PHP is outputting the source code for the HTML and
JavaScript.  So what you are trying to do is get PHP to take
dog,cat,cow from MySQL and output:

<Script Language="JavaScript">
text = new Array("dog","cat","cow")

This block of code does exactly that, with the print"\"$text\"";
filling in dog cat and cow.  I've added comments so you can
see how each line produces the above (I am using C-Style
comments as the // gets mucked up in e-mail)

The parts in [] are comments within comments to show flow
control and are not output.

/* <Script Language="JavaScript"> */
  echo "<Script Language=\"JavaScript\">\n";
/* text=new Array( */
  echo "text = new Array(";
  while ($i < $number)

    $text = mysql_result($result, $i, "name");
/* "dog" [first time] */
/* "cat" [second time] */
/* "cow" [third time] */
    print "\"$text\"";
    if ($i < $number)
/* , [first time] */
/* , [second time] */
       print ",";
/* ) [third time] */
      print ")\n";
/* </Script> */
  echo "</Script>\n";

So if you can follow that (just walk all the way through
the loop for each of the three passes) you can see that
no part of the code (including the print "\"$text\"";) is

Hope this helps

Sheridan Saint-Michel
Website Administrator
FoxJet, an ITW Company

----- Original Message -----
From: Neil Freeman <[EMAIL PROTECTED]>
To: Sheridan Saint-Michel <[EMAIL PROTECTED]>
Cc: php-general <[EMAIL PROTECTED]>
Sent: Wednesday, August 15, 2001 12:09 PM
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
> Or could it be some other problem?
> Neil
> PS: Here is my updated code:

PHP General Mailing List (
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to