Re: Creating a javascript array from database data (Off-List)

From: Date: Wed, 15 Aug 2001 18:08:27 +0000
Subject: Re: Creating a javascript array from database data (Off-List)
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-62885@lists.php.net to get a copy of this message
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") </Script> 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. else { /* <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\""; $i++; if ($i < $number) /* , [first time] */ /* , [second time] */ print ","; else /* ) [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 unnecessary Hope this helps Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com ----- Original Message ----- From: Neil Freeman <neil.freeman@asa.co.uk> To: Sheridan Saint-Michel <webmaster@foxjet.com> Cc: php-general <php-general@lists.php.net> 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 removed? > Or could it be some other problem? > > Neil > > PS: Here is my updated code: <Snip>

« previous php.general (#62885) next »